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
Michael VillegasMichael Villegas 

Unable to Scheduled Apex Class on Frequency

Hello!

Any time I attempt to schedule the following Apex Class on any frequency via either UI or Dev Console, it'll run once and then be removed from the Scheduled Jobs. 

global class CompositeProjectImportScheduler implements Schedulable
{
  global void execute(SchedulableContext sc) 
  {
      Set<Id> setApexClassId = new Set<Id>();
        for(ApexClass apexClassDetails : [Select Id, Name from ApexClass 
          where Name = 'BatchCompositeProjectProcessor'])
        {
          setApexClassId.add(apexClassDetails.id);
        }
        
        List<AsyncApexJob> lstJobs = [Select Id, Status, ApexClassID
        from AsyncApexJob where 
        (Status = 'Queued' OR Status = 'Processing' OR
         Status = 'Preparing') and ApexClassID IN: setApexClassId];

      // Make sure there are no current batch jobs running
        //
        if(lstJobs == null || lstJobs.size() == 0)
      {
            // Start the EO CP import processor batch job which will process any records 
            // that are 'readyforprocessing'.
            //
        eo2.BatchCompositeProjectProcessor BCP = new eo2.BatchCompositeProjectProcessor();
          Database.executeBatch(BCP,1);
      }
      else
      {
        system.debug('CP import batch job already in progress, please wait for the current batch job to complete.');
      }
        
        // Clean up the current scheduled job so that another can be started again later.
        //
        system.abortJob(sc.getTriggerID());
  }
    
    // Call this from your trigger to create a scheduled job which will start the CP import 
    // processor batch job
    //
    public static void scheduleProcessing()
    {
        ApexClass apexClassDetails = [Select Id, Name from ApexClass
            where Name = 'CompositeProjectImportScheduler'];

        List<AsyncApexJob> lstJobs = [Select Id, Status, ApexClassID
            from AsyncApexJob where
            (Status = 'Queued' OR Status = 'Processing' OR
             Status = 'Preparing' OR Status = '') and ApexClassID =: apexClassDetails.Id];

        // Make sure there isn't already a scheduled job.  Salesforce will only allow one 
        // scheduled job for a given class so this ensures that only one batch job is 
        // started.
        //
        if(lstJobs.size() == 0)
        {
            // Schedule job to execute one minute in the future
            //
            Datetime schedulerTime = Datetime.now().addMinutes(1);
            String strScheduleTime = '0 ' + schedulerTime.minute() + ' ' +
                schedulerTime.hour() + ' ' + schedulerTime.day() + ' '+
                schedulerTime.month() + ' ? '+ schedulerTime.year();

            CompositeProjectImportScheduler scheduler = new CompositeProjectImportScheduler();
            System.schedule('Process CP records', strScheduleTime, scheduler);
        }
        else
        {
            System.debug('CP import scheduled job already pending.');
        }
    }
    
}


My Dev Console attempts
------------------------------------------------------------------------------------
first attempt
------------------------------------------------------------------------------------

System.schedule('HourlyRun', '0 0 * * * ?', new CompositeProjectImportScheduler() );

------------------------------------------------------------------------------------
second attempt
------------------------------------------------------------------------------------
CompositeProjectImportScheduler reminder = new CompositeProjectImportScheduler();
String sch = '0 0 * * * ?';
String jobID = System.schedule('HourlyRun2', sch, reminder);


Any advice would be most appreciated!

Thanks!


Michael
Best Answer chosen by Michael Villegas
Abdul KhatriAbdul Khatri
You need to remove this line, It is aborting your job.
system.abortJob(sc.getTriggerID());

 

All Answers

Abdul KhatriAbdul Khatri
To me it looks good.

How you say it run only once, can you please share the screen shot of the scheduled apex and show the Next Scheduled Run?
Michael VillegasMichael Villegas
Here's a screenshot of the job before it ran 
User-added image

and a screenshot after it ran
User-added image
Abdul KhatriAbdul Khatri
How about when it already ran?
Michael VillegasMichael Villegas
I've included both screenshots above
Abdul KhatriAbdul Khatri
You need to remove this line, It is aborting your job.
system.abortJob(sc.getTriggerID());

 
This was selected as the best answer
Kris KrisKris Kris
Did you solve this problem?
Abdul KhatriAbdul Khatri
Was removing the line suggestion worked?
Michael VillegasMichael Villegas
Thanks, Abdul! That resolved my issue. I've marked your response as the best answer. Very much appreciated!

 
Abdul KhatriAbdul Khatri
Thanks