You need to sign in to do that
Don't have an account?
Rajesh 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
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: Please Mark It As Best Answer If It Helps
Thank You!
Greetings!
In apex, Type is a class. It is mainly used to instantiating a new type.
like-
Type 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. In the above code, you are creating an Abstract class instance for running that batch using this instance, you will run your batch.
Like- If you find your Solution then mark this as the best answer.
Thank you!
Regards,
Suraj Tripathi
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.
Then create an Apex class implements SocialApi named “FacebookApi”.
Now we can instantiate “FacebookApi” class and call its method with the value of Contact.Type__c as the following source.
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh