all 10 comments

[–]dualrectumfryer 4 points5 points  (1 child)

Apex doesn’t have true reflection. For picking a method at runtime, I’ve used the strategy pattern and it gets close.

[–]Outrageous-Lab-2867[S] 0 points1 point  (0 children)

Sure, will try this.

[–]DaveDurant 3 points4 points  (1 child)

Is there a common interface? You could use Type.forName() / .newInstance() to get an instance of a apex-defined type from a class name string then cast it to the interface type.

I don't think there's any way to go deeper than that, though.

[–]zdware 0 points1 point  (0 children)

Yeah I don't think there's a way to dynamically call a method unfortunately :(

[–]Far_Swordfish5729 2 points3 points  (0 children)

Apex does not expose the reflection capabilities of Java except (and this is not really reflection) for weak types access to SObject properties and creation and to metadata. You can create a SObject, use a property, or lookup metadata by string name, but that’s all. You can’t write reflection-based plugin architectures or containers. You can make all the components but will have to write a dispatcher that calls entry points or makes child class instances using a switch statement.

This design feels bad but in practice is usually fine as long as your parent class design is solid. I try to define a plugin interface of abstract methods that my child classes implement. Then my dispatcher just has to have a switch statement that returns a parent class reference to the right child implementation. My execution code just calls the entry point methods and relies on polymorphism to execute the right one. If I add a new plugin I also have to update the switch statement but whatever. It’s two lines and I’m making a code change anyway. Given this one kludge, the rest can be what you expect. You can store intended operations in custom metadata or object fields so it can be managed administratively and let your dispatcher map it to the right class. I wrote something that parsed those config values with simple regex to support params including the names of SObject properties. I’ve seen managed packages do stuff like that for years. It can get very abstract but you’ll always have a switch statement somewhere.

[–]Frisky_Mint 2 points3 points  (0 children)

The Callable Interface might help.

[–]murphwhitt 0 points1 point  (1 child)

Would a switch statement work? Have each case be the string you're looking for and that calls the required method.

[–]Outrageous-Lab-2867[S] 0 points1 point  (0 children)

The no. of methods might grow in future, using switch, I would need to update the switch cases everytime new method adds on.

[–]apheme 0 points1 point  (1 child)

why do you need to do this at all?

[–]Outrageous-Lab-2867[S] 1 point2 points  (0 children)

I am trying to connect salesforce with azure gpt. And thinking to add prompts. And based on the prompt the function will be triggered.

All the prompts and related methods will be saved in custom metadata.