You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Dynamically Calling Apex Class Methods


Type Class in Salesforce:

For Invoking apex classes dynamically, we need to use Type class in Salesforce. Type class in Salesforce is used to create a class instance at the runtime, and this function is primarily used to invoke different classes based on different types of user input or data from external systems or different types of handler for the DML operations. You can accomplish this dynamically by creating an instance of a class that implements an interface and by executing one of the methods of the interface.

Syntax:

forName (String)

Returns the type that corresponds to the specified fully qualified class name.

Example:

Type t = Type.forName (‘BeforeInsert’); // BeforeInsert is an Apex Class Name

In this article, I have shared my code for invoking different classes for dissimilar operations on the Task object

Step1: Create a Constants class (TriggerConstants) which is used for storing the Apex class names for invoking dynamically.

 Public class TriggerConstants {

    public static final String TaskInsert    = 'TaskInsertHandler';
    public static final String TaskUpdate  = 'TaskUpdateHandler';
    public static final String TaskDelete   = 'TaskUpdateHandler';
    public static final String TaskUnDelete = 'TaskUnDeleteHandler';

}

Step2: Create an Interface for the Apex Classes

Public interface HandlerInterface {
     void ProcessData ();
}

Step3: Create apex classes for the each operations such as (Insert, Update, Delete, Undelete)

Task Insert Handler

Public class TaskInsertHandler implements HandlerInterface {
    public void ProcessData () {
        System.debug ('-------Inside Insert Handler-------');
    }
}

Task Update Handler

Public class TaskUpdateHandler implements HandlerInterface {
        public void ProcessData () {
        System.debug ('-------Inside Update Handler-------');
    }
}

Task Delete Handler

Public class TaskDeleteHandler implements HandlerInterface {
        public void ProcessData () {
        System.debug ('-------Inside Delete Handler-------');
    }
}

Task Un Delete Handler

Public class TaskUnDeleteHandler implements HandlerInterface {
    public void ProcessData () {
        System.debug ('-------Inside UnDelete Handler-------');
    }
}

Step 4:

Create a Trigger: Trigger TaskTrigger on Task (before insert,after insert,before update, after update, before delete, after delete,after undelete) {
    Type t;
    if(trigger.isInsert){
        t = Type.forName(TriggerConstants.TaskInsert);
    } else if(trigger.isUpdate){
        t = Type.forName(TriggerConstants.TaskUpdate);
    } else if(trigger.isDelete){
        t = Type.forName(TriggerConstants.TaskDelete);
    } else if(trigger.isUnDelete){
        t = Type.forName(TriggerConstants.TaskUnDelete);
    }
    HandlerInterface v = (HandlerInterface)t.newInstance();
    v.ProcessData();
    
}

 

Here, if you do an insert operation, the TaskInsertHandler’s processData method is automatically invoked, and same happens for the update, delete and undelete operations; besides, we are not hard coding the class names for insert, update, delete and undelete operations, and the class names are assigned automatically at the runtime by using the Type class.

Also, instead of creating a new instance of each class, we derive the class name from the TriggerConstants class and use Type.forName() function to get an sObject instance of the corresponding class.

 Reference Link: Salesforce Type Class