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
DJP1SDJP1S 

Scheduled Apex Job not Working

Hey everyone, I've got this Apex class that I swear should work:

 

Global class Page1TaskReadyTriggerDate implements Schedulable {
  global void execute (Schedulablecontext SC){
    
    Page1Task__c[] tasksToUpdate = new Page1Task__c[]{};
    
  date myDate = system.today();
  
  Page1Task__c[] readyTasks = ([SELECT ID, Status__c FROM Page1Task__c 
                WHERE Ready_Trigger_Date__c != NULL
                AND Ready_Trigger_Date__c <= :myDate
                AND Status__c = '1) Not Started']);
          
  for (Page1Task__c rt : readyTasks){
    if (rt.Status__c != null){
    rt.Status__c = '2) Ready';
    tasksToUpdate.add(rt);      
    }
  }
  
  
  update tasksToUpdate;
  
  }
}

 

When I use the baked-in apex scheduler in Salesforce, it will run on the day I set it - but fails on subsequent days. For instance, I set it to run each day at 6am. It will run that day at 6am and set all of my fields accordingly. It fails on subsequent days. Is there something I'm missing or should this be rewritten?

 

Edit: It actually only runs the day the class was generated. It's not getting the system date on subsequent days, Creating a second scheduled apex job results in nothing, but rebuilding the class and scheduling works fine.

JBabuJBabu

Hi,

 

You might have to change your logic:

 

As SELECT clause is giving output for records(whose status is  '1) Not Started' )whose run trigger date is already passed.

For these records you are updating the status to '2) ready'  and for the next run you dont have any records which satisfy the SELECT clause condition.

 

Thanks,

JBabu.

DJP1SDJP1S

But we're comparing it to myDate which is system.today(), right?

 

I tried building the query without the myDate variable and instead using the SOQL TODAY variable. Same problem. What I have doen is scheduled it myself through the system log thusly:

ReadyTrigger pa= new ReadyTrigger();
String cronStr = '0 0 6 * *  ?';
System.schedule('ReadyTrigger', cronStr, pa);

 

So far so good. Remember, I couldn't even make a new scheduled apex class and have it work if it was scheduled already. This fixed my problem for the time being.

 

 

DJP1SDJP1S

Well, that didn't work. I still have tasks that are marked as 1) Not started that were created long before this class, and before I scheduled it via system log. I opened up a ticket with SF and it doesn't look like they'll be figuring out what's wrong any time soon. I see no reason why scheduled job would only work on the first pass.

sansmusicsansmusic

You got the solution? I got the same problem now. The scheduled classes are working fine in my sandbox, but were in PROD its not started at least. Even in "Apex Jobs" tab where we can see the logs for the schedules jobs, there also no entry created. Please help me out if you can. Thanks in advance...

DJP1SDJP1S

I basically ended up making 2 scheduled jobs at different times each day, it's realy annoying that only the second one each day performs it's job. I'm going to submit a support ticket as there's nothing pertaining to the code that would make it not work.

sansmusicsansmusic

That support guys are not supporting dude. i've raised a case 2 days back and till now I did'nt received any word from them. Even I called the customer support and requested them to look into my case. But no use. This is very critical issue. Don't know what to do?

Vishant ShahVishant Shah

hello mate, did you find a resolution to this? i am facing similar issue, my schedule just doesnt execute on scheduled time

Thanks
Vish