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 args)" which has a return type of Object.

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:
  • Above class has two methods (Method1 with Integer return type and, Method2 with Boolean return type) and they accept Date and Integer as input parameters

  • We have out of the box method "Call" that accepts String, Map as input parameters. Also we are using Switch statement to check for input action type to call specific method and return values


  • 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:

  • Apex Developer Guide: Callable Interface


  • Thank you!

    Comments

    1. 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

      Salesforce Custom Application Development Services

      ReplyDelete
    2. 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 .


      ReplyDelete

    Post a Comment

    Popular posts from this blog

    Lightning Data Service - Salesforce new Apex alternative - Part 2

    Using JavaScript Promises in Lightning Components

    Two-factor authentication using LWC