Callable Interface in Salesforce Apex
With Winter '19 release new Callable Interface has been introduced, which helps in calling methods dynamically between Apex classes or triggers, even for code that works in separate packages.
Class has to implement "Callable" interface along with a method "call(String action, Map
Below is the sample Apex code using callable interface using.
CallableApexTest.apx:
/* * Purpose: To test new Callable Interface * Date: OCT302018 */ public class CallableTest2 implements Callable { // Method to return month of the year Integer method1(Date d) { Integer month = d.month(); return month; } // Method to return boolean if passed integer is same as current month Boolean method2(Integer mnth) { if (method1(System.today()) == mnth) { return true; } else { return false; } } // To call specific methods dynamically public Object call(String action, Map<String, Object> args) { Switch on action { when 'Method1' { return this.method1((Date)args.get('Date')); } when 'Method2' { return this.method2((Integer)args.get('Integer')); } when else { throw new ExtensionMalformedCallException('Method not implemented'); } } } // To throw exceptions public class ExtensionMalformedCallException extends Exception {} }
Code walkthrough:
Testing for success response: Lets go ahead and test above Class code using Developer Console.When we use below piece of code, I should be able to view my results successfully.
// List to store method names List<String> lstString = new List<String> {'Method1', 'Method2'}; // Map to store specific method input parameter Map<String, Object> args = new Map<String, Object> {'Date'=>(Object)System.today(), 'Integer'=>(Object)10}; // Instantiate CallableTest class CallableTest cTest = new CallableTest(); for (String iter: lstString) { // invoking call method Object obj = cTest.call(iter, args); System.debug('************Result: ' + obj); }
Testing for exception response: When we use below piece of code, I should be able to view exception message.
// List to store method names List<String> lstString = new List<String> {'Method5'}; // Map to store specific method input parameter Map<String, Object> args = new Map<String, Object> {'Date'=>(Object)System.today(), 'Integer'=>(Object)10}; // Instantiate CallableTest class CallableTest cTest = new CallableTest(); for (String iter: lstString) { // invoking call method Object obj = cTest.call(iter, args); System.debug('************Result: ' + obj); }
Resource Reference Links:
Thank you!


I Enjoyed Reading The Blog. I love your blog. Thanks for sharing all of the useful info and will come back to read more. Visit:- Salesforce Lightning Consulting Services
ReplyDeleteSalesforce Custom Application Development Services
Thanks for sharing this great information I am impressed by the information that you have on this blog. Same as your blog i found another one Oracle Fusion Financials . Actually I was looking for the same information on internet for Oracle Fusion Financials and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject, you can learn more aboutOracle Fusion Financials . By attending Oracle Fusion FinancialsTraining .
ReplyDeletebuy online CIALIS 10MG
ReplyDeleteBuy Tramadol 100 mg Online
ReplyDeleteBuy Ambien Online Overnight
ReplyDelete