+ Start a Discussion
Steve BerleySteve Berley 

pass class method name as parameter to another method?

Hi - 

I've got a circumstance where I need to pass the name of a class method to another method which would execute then passed method.

Example:
the class I'd want to call:
public class1 {
    public static void methodA() {
    };
}

The class I'd pass the method name to...
public class2 {
   public static void methodRunner(string methodName) {
      run( class1.methodName() );  // call that would execute the method (TBD)
   }
}

so the call would be - 
class2.methodRunner( class1.methodA );

In case you're wondering why go to all this work? 
I'd like to create a generic way to run class methods as batch processes from the console in prod to efficiently retrofit existing data - without littering the prod org with lots of one-time use code.

Looking forward to everyone's collective insights...

Thanks,

Steve 
Best Answer chosen by Steve Berley
Alain CabonAlain Cabon
Hi,

Apex is not a functional language at all. You have to deal with the classic OO design patterns like the Strategy pattern - defining a family of algorithms, enscapsulating each one and making them interchangeable and selectable at runtime. 

https://developer.salesforce.com/page/Apex_Design_Patterns  (https://developer.salesforce.com/page/Apex_Design_Patterns )

The best pattern for your need is the Command pattern (not explained in the link above).

https://en.wikipedia.org/wiki/Command_pattern

That is not your real need . 

Javascript is almost a functional language and it is used for the controllers in Lightning.

https://salesforce.stackexchange.com/questions/11865/build-custom-batch-queue-to-centrally-manage-batches-circumvent-the-max-5-bat

Download the sample code and metadata of: Advanced Apex: 

http://advancedapex.com/samplecode/

There is a "trick" using the reflection in Apex, brought to you by JSON (Dynamic Class Instantiation with JSON).

https://zachelrath.wordpress.com/2012/10/21/apex-reflection-brought-to-you-by-json/

There is no reflection api in Apex natively.  https://success.salesforce.com/ideaView?id=08730000000BrVaAAK

There is just the system Type:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_type.htm

Regards

All Answers

Alain CabonAlain Cabon
Hi,

Apex is not a functional language at all. You have to deal with the classic OO design patterns like the Strategy pattern - defining a family of algorithms, enscapsulating each one and making them interchangeable and selectable at runtime. 

https://developer.salesforce.com/page/Apex_Design_Patterns  (https://developer.salesforce.com/page/Apex_Design_Patterns )

The best pattern for your need is the Command pattern (not explained in the link above).

https://en.wikipedia.org/wiki/Command_pattern

That is not your real need . 

Javascript is almost a functional language and it is used for the controllers in Lightning.

https://salesforce.stackexchange.com/questions/11865/build-custom-batch-queue-to-centrally-manage-batches-circumvent-the-max-5-bat

Download the sample code and metadata of: Advanced Apex: 

http://advancedapex.com/samplecode/

There is a "trick" using the reflection in Apex, brought to you by JSON (Dynamic Class Instantiation with JSON).

https://zachelrath.wordpress.com/2012/10/21/apex-reflection-brought-to-you-by-json/

There is no reflection api in Apex natively.  https://success.salesforce.com/ideaView?id=08730000000BrVaAAK

There is just the system Type:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_type.htm

Regards
This was selected as the best answer
Steve BerleySteve Berley
Thank you @Alain!  I have to say this is the best response EVER to a posting.

Yes, the Advanced Apex book is fantastic and I love the Design Patterns paper as well.

Steve