Create Dynamic Apex Instance using Type.forName()

In this post, we will implement the functionality to Create Dynamic Apex Instance. This is mostly required when we have a method of Interface that is implemented by multiple Apex Classes and we have to call it dynamically. To create a Dynamic Apex Instance, we have to make use of Type.forName(). Let’s hop into the implementation with a common scenario.

Implementation

Create Dynamic Apex Instance using Type.forName()
Create Dynamic Apex Instance using Type.forName()

First, create Interface FruitInterface with method printFruitName() without any parameters.

FruitInterface.apxc

public interface FruitInterface {
	void printFruitName();
}

Create Apex Class MangoFruit and OrangeFruit and implement FruitInterface Interface. Provide body for the method printFruitName(). This method will simply display the name of Fruit.

MangoFruit.apxc

public class MangoFruit implements FruitInterface {
    public void printFruitName(){
        System.debug('This is Mango Fruit');
    }
}

OrangeFruit.apxc

public class OrangeFruit implements FruitInterface {
	public void printFruitName(){
        System.debug('This is Orange Fruit');
    }
}

Create Dynamic Apex Instance

To create a Dynamic Apex Instance, we have to make use of Type.forName().

First, create an Apex Class FruitController with a method getFruit() which will accept the name of the class as a parameter. If the parameter is MangoFruit, it should call the printFruitName() method of MangoFruit class. If the parameter is OrangeFruit, it should call the printFruitName() method of OrangeFruit class. For this, we usually write code with multiple if conditions, something like below:

FruitController.apxc

public class FruitController {
    public static void getFruit(String className){
        FruitInterface objFruit;
        if(className == 'MangoFruit'){
            objFruit = new MangoFruit();
        } else if(className == 'OrangeFruit'){
            objFruit = new OrangeFruit();
        }
        objFruit.printFruitName();
    }
}

But this implementation is not scalable. In the future, we might need to add another Fruit Class that implements the FruitInterface, let’s say BananaFruit. In this case, we need to make a change in FruitController and add another if condition to handle the instance of BananaFruit.

Improved FruitController using Type.forName()

In such cases, we can make use of Type.forName(). Call forName() method of Type Class and pass the name of the Class as a parameter. This will return the instance of Type. Then, call the newInstance() method using Type instance to get the instance of Apex Class that is passed as a parameter.

public class FruitController {
    public static void getFruit(String className){	
        Type dynamicApexType = Type.forName(className);
		FruitInterface objFruit = (FruitInterface) dynamicApexType.newInstance();
        objFruit.printFruitName();
    }
}

No matter how many classes implements FruitInterface, this code will run without making any changes.

This is how we can make use of Type.forName() method to create a Dynamic Apex Instance.

Please Subscribe here if you don’t want to miss new implementations. If you want to know more about Type Apex Class, you can check official Salesforce documentation here.

Some hand-picked implementations for you:

See you in the next implementation! Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *