function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AngulixAngulix 

method name as argument...

Hi,
I need to pass a method name as argument. How can I do that ?
This is my practical case, the String.join() can only read list of String. But I want it to work for a list of object (in my case: cMyObject). The object will have a function to send back the proper string (here: getFormattedStr()). I want to make a function "getListOfStringForJoin" that receive the function getFormattedStr() as argument. As Apex is object oriented, I guess it is possible.

public class cMyObject {
... all stuff as constructor, internal properties...
public String getFormattedStr() {
return ... a Formatted Str ...;
}
}
 
list<cMyObject> vListMyObject = new list<cMyObject>();

public list<String> getListOfStringForJoin( list<object> pList, function pFuncName ) {
list<String> tOutList = new list<String>();
for( Integer tIter = 0; tIter < pList.size(); tIter++) {
tOutList.add( pList[ tIter ].pFuncName() );
}
return tOutList;
}

outStr = join( getListOfStringForJoin( vListMyObject, getFormattedStr ), ', ' );

Thanks a lot for tips and thoughts !
logontokartiklogontokartik
Dynamic Methods are not yet available in Salesforce. The max reflection you can get is upto a class, its attributes and methods are not yet dynamic.