You need to sign in to do that
Don't have an account?
How to create a dynamic instance for Schedulable Apex Class
Hello,
This is my first post, please help me on the below issue.
There is a custom object which stores the Schedulable Apex Classes. Requirement is to create a Schedulable apex jobs for all of them using System.schedule method but it allows only instace of a Schedulable apex class as a input parameter, below is my code. Please advise how to create a dynamic instace for the sch.apex class which is available at run time in variable wa.Apex_Class__c.
QJobsList = [SELECT id, Name, Job_Name__c, Apex_Class__c FROM Schedule_Apex__c ];
for(Schedule_Apex__c wa : QJobsList){
wa.Apex_Class__c Obj = new wa.Apex_Class__c(); //This Statment is not working, Seems to be a dynamic assignment is required
//Cron Expression
String sch = '0 0 0 '+ wa.Scheduled_Date__c.day() + ' ' + wa.Scheduled_Date__c.month() + ' ? ' + wa.Scheduled_Date__c.year(); //
System.schedule( 'Test job1' , sch , Obj);
}
If i can give write like this TestSchApxJob c = new TestSchApxJob(); //schedulable apex class name, there is no error but it's not possible
I have googled this and found i can use something like below but still not able to understand.
// Get the Type corresponding to the class name
Type t = Type.forName(cs.className__c);
Thanks in advance for your help,
Suman
This is my first post, please help me on the below issue.
There is a custom object which stores the Schedulable Apex Classes. Requirement is to create a Schedulable apex jobs for all of them using System.schedule method but it allows only instace of a Schedulable apex class as a input parameter, below is my code. Please advise how to create a dynamic instace for the sch.apex class which is available at run time in variable wa.Apex_Class__c.
QJobsList = [SELECT id, Name, Job_Name__c, Apex_Class__c FROM Schedule_Apex__c ];
for(Schedule_Apex__c wa : QJobsList){
wa.Apex_Class__c Obj = new wa.Apex_Class__c(); //This Statment is not working, Seems to be a dynamic assignment is required
//Cron Expression
String sch = '0 0 0 '+ wa.Scheduled_Date__c.day() + ' ' + wa.Scheduled_Date__c.month() + ' ? ' + wa.Scheduled_Date__c.year(); //
System.schedule( 'Test job1' , sch , Obj);
}
If i can give write like this TestSchApxJob c = new TestSchApxJob(); //schedulable apex class name, there is no error but it's not possible
I have googled this and found i can use something like below but still not able to understand.
// Get the Type corresponding to the class name
Type t = Type.forName(cs.className__c);
Thanks in advance for your help,
Suman
The only possible solution to make it completely dynamic is by using interfaces. Use something like this:
Let me know if this helps.
Thanks,
Manish
Check out my blog http://sfdcfacts.com/
All Answers
I have tried with your code, still it throwing error..
1. Invlaid type wa.Apex_class__c
2. Method doesn't exist or incorrect signature..
Any other idea ? please..
I am afraid this will not be possible. Your custom object stores the class name which is a string data type. You cannot convert/typecast any datatype to class datatype except sObject and Object.
> In your case you are trying to create an instance of a class using its name which is a String.
> The Name String does not hold any object properties of that class and it is stored as character array in the memory.
> What you need to have is Class Datatype instead of String Data type to create the instance of the class.
Please create class datatype from String like below:
Apart from above solution, there is no mechanism in Apex to create instance of a class using string.
But the challenge is above solution is that if you want to typecast your class, then again you will need to use 2nd line and simply giving class name will not work here as system needs class datatye instead of class name.
Let me know if this helps
Thanks,
Manish
Check out my blog http://sfdcfacts.com/
The only possible solution to make it completely dynamic is by using interfaces. Use something like this:
Let me know if this helps.
Thanks,
Manish
Check out my blog http://sfdcfacts.com/