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
Matthew HoldgateMatthew Holdgate 

scheduled apex not running but showing in the jobs

Hi Everyone,

I have an apex class that I have developed and tested that it works by calling it from a JavaScript button but now I want to schedule it to run daily to prevent someone having to press the button. My code is
 
global class DOUM_RetireDOUs implements Schedulable{

    global static boolean repeatedDOU(DOUM_DOU__c obj,List<DOUM_DOU__c> objects,String DOUName){ 
       
        for(DOUM_DOU__c rec: objects){
            if(obj.Id == rec.Id){
                continue;
            }
            else{
                if(DOUName == rec.DOUM_DOU_Name__c){
                    //if(obj.DOUM_DOUVersion__c > 1){
                        return true;                    
                    //}
                }
            }
        }
        return false;
    }
    
    
    
   webService static void checkUpdate(){
    
        List<DOUM_DOU__c> objects = [SELECT Id, Name, DOUM_DOUVersion__c, DOUM_Domain__c, DOUM_Requester__r.Name, DOUM_DOU_Approval_Status__c, DOUM_DOU_Name__c, DOUM_DofRetire__c, DOUM_RfR__c, DOUM_RetirePerson__c, DOUM_Retired__c FROM DOUM_DOU__c WHERE DOUM_DOU_Approval_Status__c ='Approved'];
        
        for(DOUM_DOU__c obj: objects){
        
    Integer Version = Integer.ValueOf(obj.DOUM_DOUVersion__c) + 1;
    String OldVersNo = String.ValueOf(Version);
    String Domain = obj.DOUM_Domain__c;
    String JoinDOU = '-DOU-';
    String Requester = obj.DOUM_Requester__r.Name;
    String JoinVers = ' V';
    String DOUName = Domain + JoinDOU + Requester + JoinVers + OldVersNo;
    
        system.debug(Domain);        
        system.debug(Version);
        system.debug(Requester);
        system.debug(OldVersNo);
            
        system.debug(DOUName);
        
        
        System.debug('if statatment'+repeatedDOU(obj,objects,DOUName));
            
        System.debug('obj dou name '+obj.DOUM_DOU_Name__c);
        System.debug('Dou name '+DOUName);
            
            if(repeatedDOU(obj,objects,DOUName)){
                  obj.DOUM_DofRetire__c = Date.today();
                  obj.DOUM_RfR__c = 'New Version Approved';
                  obj.DOUM_RetirePerson__c = String.ValueOf(obj.CreatedBy.Id); 
                  obj.DOUM_Retired__c = TRUE;
                  update obj;
            }
        }
    }
    
    global void execute(SchedulableContext sc) {
    
        DOUM_RetireDOUs CU = new DOUM_RetireDOUs();
        
        String DOU = '0 0 0 ? * MON-FRI';
        String jobID = System.schedule('RetireDOUs', DOU, CU);

        
   }
In the schedule apex I have set it to run Mon-Fri at prefered time of 00:00. In the list view of scheduled jobs my apex shows that it was submitted but never started. As well as this the code hasnt run and changed my data.

Any ideas?


 
Charisse de BelenCharisse de Belen
Hi Matthew,

I don't think you should be trying to run the scheduled Apex from within its own execute method. Instead, use the execute method to call your checkUpdate method, and then use the built-in UI to schedule the job. To do this, go to Setup > Apex Classes, and then click on Schedule Apex.