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
Rajesh T 19Rajesh T 19 

What is the purpose of using Type.forName() in Salesforce Apex ?

When I gone through the below code which already present in the system , I couldn't understand the function. Can anyone help me

Code in the Apex class:

Type theBatchType = Type.forName( con.BatchClass__c );
theBatch = ( Abstractclass )theBatchType.newInstance();
// Here con.BatchClass__c class extends the 

CharuDuttCharuDutt
Hii Rajesh 

Usage. Use the forName methods to retrieve the type of an Apex class, which can be a built-in or a user-defined class. You can use these methods to retrieve the type of public and global classes, and not private classes even if the context user has access

For Example:
Type t = Type.forName('YourClassName'); //Create a type object using class name 

YourClassName newObj = (YourClassName)t.newInstance(); //create a instance using Type object and TypeCast to your class

Please Mark It As Best Answer If It Helps
Thank You!

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Rajesh,
Greetings!

In apex, Type is a class. It is mainly used to instantiating a new type.
like-
Type
theBatchType = Type.forName(con.BatchClass__c);
In the above code, there is a text field (BatchClass__c) in contact, which stores the name of the Abstract class.
So you are creating a typical instance of the class whose name is stored in the text field.
theBatch = ( Abstractclass )the BatchType.newInstance();
In the above code, you are creating an Abstract class instance for running that batch using this instance, you will run your batch.
Like-
Database.executeBatch(theBatch);
If you find your Solution then mark this as the best answer. 

Thank you!
Regards,
Suraj Tripathi
mukesh guptamukesh gupta
Hi Rajesh,

Please try to uderstand below example:-

Somtimes we need to instantiate a Apex class by dynamic value in Salesforce, which can make our program more modular and more readable, such as the below case :

1. Each Contact has its socail fields : “Type__c” and “AccessToken__c”
2. Needs to request its profile via socail api in Apex

First we need to create an interface class, e.g. SocialApi, as the following source :
※A parent class with keyword “virtual” is workable as well.
 
public interface SocialApi {

  String getProfile(String accessToken);

}

Then create an Apex class implements SocialApi named “FacebookApi”.
public class FacebookApi implements SocialApi{
	
  public String getProfile(String accessToken) {
    // Request profile via Facebook api
    return 'https://scontent.xx.fbcdn.net/xxxxxx.png';
  }

}

Now we can instantiate “FacebookApi” class and call its method with the value of Contact.Type__c as the following source.
Contact contact = [SELECT Id, Type__c, AccessToken__c FROM Contact LIMIT 1];
System.assertEquals(contact.Type__c, 'Facebook');

String socialType = contact.Type__c + 'Api';
Type t = Type.forName(socialType);
SocialApi api = (SocialApi)t.newInstance();
String profile = api.getProfile(contact.AccessToken__c);
System.assertEquals(profile, 'https://scontent.xx.fbcdn.net/xxxxxx.png');

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh