0
private static void scheduleTEst(Set<Id> sObjectId, String cronJobDetailName, String className, Integer hour) {
    
      List<CronTrigger> scheduledExecs = [SELECT Id,  CronJobDetail.Name, NextFireTime, 
                                                State, StartTime, EndTime, CronExpression, 
                                                TimesTriggered 
                                                FROM CronTrigger 
                                                WHERE CronJobDetail.Name = :cronJobDetailName
                                                AND State = 'WAITING'];


    if(scheduledExecs.isEmpty()) {

      //Getting time.
      Datetime setTime = Datetime.now();

      System.Type objType = Type.forName(className);

      Object o = objType.newInstance();
      
      //Setting time.
      Datetime newSetTime = setTime.addHours(hour);

              //To String.
              String batchHour = String.valueOf(newSetTime.hour());

              //CronExpression. The format is:

              //Seconds Minutes Hours Day_of_month Month Day_of_week

              String sch = '0 0 ' + batchHour +' * * ?';

      
      //Setting schedule.
      System.schedule( className + ' Schedule', sch , o); //This line throws an error.

    }
    
}

I can save this code because I have following error:

Method does not exist or incorrect signature: void schedule(String, String, Object) from the type System

I tried to use a NEW specific batch class name (for example, 'OpportunityBatch newOppB = new OpportunityBatch()'), and it accepts it as a parameter. Why it does not accept passed the newInstance of object as a paremeter? Is there any way to do it without requirement of specify a singular class?

1 Answer 1

4

System.schedule() expects the third parameter to be something that implements the Schedulable interface (which Object does not).

Provided the class you're dynamically instantiating does implement that interface, you should be able to change

from: Object o = objType.newInstance();

to: Schedulable o = (Schedulable)objType.newInstance();

which should be enough to satisfy System.schedule()

Classes that extend interfaces can be cast as an instance of that interface (similar to how child classes can be cast as an instance of the parent class(es) they inherit from).

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .