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
Suman sfdcSuman sfdc 

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

      
                      
Best Answer chosen by Suman sfdc
Manish  ChoudhariManish Choudhari
Hi Suman, 

The only possible solution to make it completely dynamic is by using interfaces. Use something like this:
 
QJobsList = [SELECT id, Name,  Job_Name__c, Apex_Class__c FROM Schedule_Apex__c ];
for(Schedule_Apex__c wa : QJobsList){
   Type t = Type.forName(wa.Apex_Class__c); //create Type instance
   Schedulable classInstance = (Schedulable) t.newInstance(); //Create schedulable interface instance

   //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 , classInstance); //pass schedulable interface instance here
}



Let me know if this helps.

Thanks,
Manish
Check out my blog http://sfdcfacts.com/

All Answers

Raj VakatiRaj Vakati
Can you try some thing  like this 
 
ApexClass Obj  =(ApexClass) new wa.Apex_Class__c();

 
Suman sfdcSuman sfdc
Thanks for the reply Raj.
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..
Suman sfdcSuman sfdc
QJobsList = [SELECT id, Name, Job_Name__c, Apex_Class__c FROM Schedule_Apex__c ];

for(Schedule_Apex__c  wa : QJobsList){
       ApexClass Obj  =(ApexClass) new wa.Apex_Class__c();
         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);
}

This is what i tried Raj..
Raj VakatiRaj Vakati
try this 
 
QJobsList = [SELECT id, Name, Job_Name__c, Apex_Class__c FROM Schedule_Apex__c ];

for(Schedule_Apex__c  wa : QJobsList){
          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 , wa.Apex_Class__c);
}

 
Raj VakatiRaj Vakati
QJobsList = [SELECT id, Name, Job_Name__c, Apex_Class__c FROM Schedule_Apex__c ];

for(Schedule_Apex__c  wa : QJobsList){
          String sch = '0 0 0 '+ wa.Scheduled_Date__c.day() + ' ' + wa.Scheduled_Date__c.month() + ' ? ' + wa.Scheduled_Date__c.year();
             
        System.schedule( 'Test job1'+wa.Name , sch , wa.Apex_Class__c);
}

 
Suman sfdcSuman sfdc
No Raj, it's not working. i have already tried that.. the syntax of the system.schedule is (jobName, Cron Expre, InstanceOfSch.Class)..
Manish  ChoudhariManish Choudhari
Hi Suman,

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:
Type t = Type.forName(wa.Apex_Class__c);//This line will create a Type Object
MyCustomClass newObj = (MyCustomClass)t.newInstance();//you can get the instance in this way and typecast to your class data type


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/
 
Manish  ChoudhariManish Choudhari
Hi Suman, 

The only possible solution to make it completely dynamic is by using interfaces. Use something like this:
 
QJobsList = [SELECT id, Name,  Job_Name__c, Apex_Class__c FROM Schedule_Apex__c ];
for(Schedule_Apex__c wa : QJobsList){
   Type t = Type.forName(wa.Apex_Class__c); //create Type instance
   Schedulable classInstance = (Schedulable) t.newInstance(); //Create schedulable interface instance

   //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 , classInstance); //pass schedulable interface instance here
}



Let me know if this helps.

Thanks,
Manish
Check out my blog http://sfdcfacts.com/
This was selected as the best answer
Suman sfdcSuman sfdc
Great Manish!! Hats off to you :) You are such an amazing developer... It's working as expected.... I have asked many people, few said it's not possible but you made it possible... I like your blogs, please keep wirting and share your knowledge. Thanks a lot for your quick help..