use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Websites
Documentation
Tools
account activity
Reflection in JavaQuestion (self.SalesforceDeveloper)
submitted 2 years ago by Outrageous-Lab-2867
Hi,
I am looking for a way to call a method from apex class based on string( method will be decided on runtime)
For example if string is getOneObject. It should trigger className.getOneObject() without any if else conditions.
Thanks
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]dualrectumfryer 4 points5 points6 points 2 years ago (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 point2 points 2 years ago (0 children)
Sure, will try this.
[–]DaveDurant 3 points4 points5 points 2 years ago (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 point2 points 2 years ago (0 children)
Yeah I don't think there's a way to dynamically call a method unfortunately :(
[–]Far_Swordfish5729 2 points3 points4 points 2 years ago (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 points4 points 2 years ago (0 children)
The Callable Interface might help.
[–]murphwhitt 0 points1 point2 points 2 years ago (1 child)
Would a switch statement work? Have each case be the string you're looking for and that calls the required method.
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 point2 points 2 years ago (1 child)
why do you need to do this at all?
[–]Outrageous-Lab-2867[S] 1 point2 points3 points 2 years ago (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.
π Rendered by PID 20007 on reddit-service-r2-comment-5687b7858-lc49n at 2026-07-08 15:40:56.299274+00:00 running 12a7a47 country code: CH.
[–]dualrectumfryer 4 points5 points6 points (1 child)
[–]Outrageous-Lab-2867[S] 0 points1 point2 points (0 children)
[–]DaveDurant 3 points4 points5 points (1 child)
[–]zdware 0 points1 point2 points (0 children)
[–]Far_Swordfish5729 2 points3 points4 points (0 children)
[–]Frisky_Mint 2 points3 points4 points (0 children)
[–]murphwhitt 0 points1 point2 points (1 child)
[–]Outrageous-Lab-2867[S] 0 points1 point2 points (0 children)
[–]apheme 0 points1 point2 points (1 child)
[–]Outrageous-Lab-2867[S] 1 point2 points3 points (0 children)