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
DannyK88DannyK88 

Apex Class Scheduled but does not run

Hi Everyone,

I have created a Apex class that I would like to schedule to run. However when I try to schedule the class it does not run. 
I have run my test class to see if there is antying wrong but it seems to run fine with the test class. I don't understand what the issue is.
Currently in our salesforce org we have a thrid party product that runs a apex class every hour. Could that be causing an issue with my class? Is it possible the two classes can't run at the same time?

Let me know if there is anything else you need from me.

Thanks,
Best Answer chosen by DannyK88
DannyK88DannyK88
This might have not been the realy issue but when I reduced the number of contacts I queried it ran fine. I decided to do that after I looked at some debug logs. It looked like I was running over the CPU time limt. When I reduced the number of records it looked at the cpu time dropped and the program ran fine. If you see anything else that could cause the issue plesae let me know. For now I will mark this as completed.

All Answers

ryanjuptonryanjupton
I think we'll need some more detail. Can you post some code? When you say it doesn't run, does it never start? Starts but generates an error? More details please.
DannyK88DannyK88
It seems to never start, I'm not sure how to check if it generates and error? How would I check if it generates a error?
When I run my test class it covers all my code and runs through everything fine. However when I schedule the class it does not seem to start the process.

Apex Code:

/*
    This is an automated process that will delete contacts based
    on some set rules to check if the contacts are candidates or not
*/
global class JunkContactDeleteClass implements Schedulable{
    // This is for the test method
    public static String CRON_EXP = '0 0 0 3 9 ? 2022';
    global void execute(SchedulableContext ctx) {
       
        List<Contact> ConList = new List<Contact>();
        String ErrorMessage = '';
        List<Contact> ConDelList = new List<Contact>();
        List<Recording_Object__c> RecObjList = new List<Recording_Object__c>();
        List<AVTRRT__Job_Applicant__c> JobAppDelList = new LIst<AVTRRT__Job_Applicant__c>();
        List<AVTRRT__ETCObject__c> ETCObjDelList = new List<AVTRRT__ETCObject__c>();
        List<Id> ChatterIdDelList = new List<Id>();
        map<Id, Schema.Recordtypeinfo> rMap = new map<Id, Schema.Recordtypeinfo>();
     Schema.DescribeSObjectResult R = Contact.SObjectType.getDescribe();
     rMap = R.getRecordTypeInfosById();
       
        // This code will delete contacts if they don't match any of the exceptions
        try{ConList = [Select Id, FirstName, LastName, AVTRRT__Resume__c, Title, Delete_Request__c, AVTRRT__Chatter_Id__c, ICIMS_Create_Date__c,
                      Email, AVTRRT__Other_Emails__c, Others__c, Delete_Request_By__c, Delete_Request_Date__c, RecordTypeId FROM Contact
                      WHERE Delete_Request__c = :true];}
        catch(exception e1){}
       
        List<Id> ConIdList = new List<Id>();
        for(Contact C: ConList){
            ConIdList.add(C.Id);
        }
       
        map<string,List<AVTRRT__Job_Applicant__c>> JobAppList = new map<string,List<AVTRRT__Job_Applicant__c>>();
        for(AVTRRT__Job_Applicant__c JA :[SELECT Id, name, AVTRRT__Contact_Candidate__c, AVTRRT__Stage__c, AVTRRT__Job__c, AVTRRT__Job__r.Name,
                         AVTRRT__Recruiter__c, AVTRRT__Recruiter__r.FirstName, AVTRRT__Recruiter__r.LastName, AVTRRT__Account_Job__c,
                         AVTRRT__Account_Job__r.Name, AVTRRT__Account_Manager__c, AVTRRT__Account_Manager__r.FirstName, AVTRRT__Account_Manager__r.LastName,
                         AVTRRT__Contact_Candidate__r.FirstName, AVTRRT__Contact_Candidate__r.LastName, CreatedDate
                         FROM AVTRRT__Job_Applicant__c WHERE AVTRRT__Contact_Candidate__c IN :(ConIdList)]){
                                              if(JobappList.get(JA.AVTRRT__Contact_Candidate__c) == null){
                                                  List<AVTRRT__Job_Applicant__c> JAList = new List<AVTRRT__Job_Applicant__c>();
                                                  JobAppList.put(JA.AVTRRT__Contact_Candidate__c, JAList);
                                                  JobAppList.get(JA.AVTRRT__Contact_Candidate__c).add(JA);
                                              }
                                              else{
                                                  JobAppList.get(JA.AVTRRT__Contact_Candidate__c).add(JA);
                                              }
        }
       
        map<string,List<AVTRRT__Placement__c>> PlaceList = new map<string,List<AVTRRT__Placement__c>>();
        for(AVTRRT__Placement__c P :[SELECT Id, Name, AVTRRT__Contact_Candidate__c FROM AVTRRT__Placement__c
                                     WHERE AVTRRT__Contact_Candidate__c IN :(ConIdList)]){
                                         if(PlaceList.get(P.AVTRRT__Contact_Candidate__c) == null){
                                             List<AVTRRT__Placement__c> PList = new List<AVTRRT__Placement__c>();
                                             PlaceLIst.put(P.AVTRRT__Contact_Candidate__c,PList);
                                             PlaceList.get(P.AVTRRT__Contact_Candidate__c).add(P);
                                         }
                                         else{
                                             PlaceList.get(P.AVTRRT__Contact_Candidate__c).add(P);
                                         }
        }
       
        map<string,List<AVTRRT__ETCObject__c>> ETCList = new map<string,List<AVTRRT__ETCObject__c>>();
        for(AVTRRT__ETCObject__c E :[SELECT Id, AVTRRT__Candidate__c FROM AVTRRT__ETCObject__c
                                   WHERE AVTRRT__Candidate__c IN:(ConIdList)]){
                                       if(ETCList.get(E.AVTRRT__Candidate__c) == null){
                                           List<AVTRRT__ETCObject__c> EList = new List<AVTRRT__ETCObject__c>();
                                           ETCList.put(E.AVTRRT__Candidate__c,EList);
                                           ETCList.get(E.AVTRRT__Candidate__c).add(E);
                                       }
                                       else{
                                           ETCList.get(E.AVTRRT__Candidate__c).add(E);
                                       }
        }
       
        for(Contact C: ConList){
            Boolean Add_Place = true;
            Boolean Add_JobApp = true;
           
            if(PlaceList.get(C.Id) != null && PlaceList.get(C.Id).size() != 0){
                if(ErrorMessage == '')
                 ErrorMessage = 'The Contact with Id \"' + C.Id + '\" because there is a Placement attached to it.\n\n';
                else
                    ErrorMessage += 'The Contact with Id \"' + C.Id + '\" because there is a Placement attached to it.\n\n';
                Add_Place = false;
            }
           
            if(JobAppList.get(C.Id) != null && JobAppList.get(C.Id).size() != 0){
                for(AVTRRT__Job_Applicant__c JA :JobAppList.get(C.Id)){
                    if(JA.AVTRRT__Stage__c != 'New Application' && JA.AVTRRT__Stage__c != 'Short Listed' &&
                      JA.AVTRRT__Stage__c != 'Screened' && JA.AVTRRT__Stage__c != 'Rejected by Candidate' &&
                      JA.AVTRRT__Stage__c != 'Rejected by Recruiter'){
                        if(ErrorMessage == '')
                   ErrorMessage = 'The Contact with Id \"' + C.Id + '\" because there is an active Job Applicant attached to it with the Id \"' + JA.Id
                                                  + '\".\n\n';
                  else
                      ErrorMessage += 'The Contact with Id \"' + C.Id + '\" because there is an active Job Applicant attached to it with the Id \"' + JA.Id
                                                  + '\".\n\n';
                        Add_JobApp = false;
                    }
                }
               
                if(Add_JobApp == true && Add_Place == true)
                    JobAppDelList.addAll(JobAppList.get(C.Id));
            }
           
            if(ETCList.get(C.Id) != null && ETCList.get(C.Id).size() != 0){
                if(Add_Place == true && Add_JobApp == true)
                    ETCObjDelList.addAll(ETCList.get(C.Id));
            }
            /*
    ICIMS_Create_Date__c,
                Email, AVTRRT__Other_Emails__c, Others__c, Delete_Request_By__c, Delete_Request_Date__c
   */
            if(Add_Place == true && Add_JobApp == true){
                ConDelList.add(C);
                Recording_Object__c RO = new Recording_Object__c();
                RO.Name = C.FirstName + ' ' + C.LastName;
                RO.Object_Type__c = 'Contact';
                RO.Record_Type__c = rMap.get(C.RecordTypeId).getName();
                RO.Data_Field_1__c = C.Delete_Request_By__c;
                if(C.Delete_Request_Date__c != null)
                 RO.Data_Field_2__c = C.Delete_Request_Date__c.format('MM/dd/yyyy');
                RO.Data_Field_3__c = C.AVTRRT__Other_Emails__c;
                RO.Data_Field_4__c = C.Email;
                RO.Data_Field_5__c = C.Others__c;
                if(C.ICIMS_Create_Date__c != null)
                 RO.Data_Field_6__c = C.ICIMS_Create_Date__c.format('MM/dd/yyyy');
                RecObjList.add(RO);
                if(C.AVTRRT__Chatter_Id__c != null)
                 ChatterIdDelList.add(Id.valueOf(C.AVTRRT__Chatter_Id__c));
            }
        }
       
        JobAppList = new map<string,List<AVTRRT__Job_Applicant__c>>();
        PlaceList = new map<string,List<AVTRRT__Placement__c>>();
        ETCList = new map<string,List<AVTRRT__ETCObject__c>>();
        ConList = new List<Contact>();
        List<Id> ConDocIdList = new List<Id>();
       
        for(ContentVersion CV :[SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE Id In:(ChatterIdDelList)]){
         ConDocIdList.add(CV.ContentDocumentId);
        }
       
        List<ContentDocument> ConDocList = new List<ContentDocument>();
        try{ConDocList = [SELECT Id FROM ContentDocument WHERE Id IN:(ConDocIdList)];}
        catch(exception e){}
       
        for(AVTRRT__Job_Applicant__c JA: JobAppDelList){
            Recording_Object__c RO = new Recording_Object__c();
            RO.Name = JA.Name;
            RO.Object_Type__c = 'Job Applicant';
            RO.Data_Field_1__c = JA.AVTRRT__Stage__c;
            RO.Data_Field_2__c = JA.AVTRRT__Job__r.Name;
            if(JA.CreatedDate != null)
                RO.Data_Field_3__c = JA.CreatedDate.format('MM/dd/yyyy');
            if(JA.AVTRRT__Contact_Candidate__c != null){
                RO.Data_Field_4__c = JA.AVTRRT__Contact_Candidate__r.FirstName + ' ' + JA.AVTRRT__Contact_Candidate__r.LastName;
             RO.Data_Field_4__c = RO.Data_Field_4__c.replace('null', '');
            }
            if(JA.AVTRRT__Account_Manager__c != null){
    RO.Data_Field_5__c = JA.AVTRRT__Account_Manager__r.FirstName + ' ' + JA.AVTRRT__Account_Manager__r.LastName;
             RO.Data_Field_5__c = RO.Data_Field_5__c.replace('null', '');
            }
            if(JA.AVTRRT__Recruiter__c != null){
             RO.Data_Field_6__c = JA.AVTRRT__Recruiter__r.FirstName + ' ' + JA.AVTRRT__Recruiter__r.LastName;
             RO.Data_Field_6__c = RO.Data_Field_6__c.replace('null', '');
            }
            RecObjList.add(RO);
        }
       
        try{Delete ETCObjDelList;}
        catch(exception e1){}
        try{Delete JobAppDelList;}
        catch(exception e2){}
        try{Delete ConDocList;}
        catch(exception e3){}
        try{Delete ConDelList;}
        catch(exception e4){}
        try{insert RecObjList;}
        catch(exception e5){}
       
        Messaging.SingleEmailMessage emailMessage = new Messaging.SingleEmailMessage();
        List<String> toEmails = new List<String>();
        toEmails.add('MyEmailAddress');
        emailMessage.setToAddresses(toEmails);
        emailMessage.setPlainTextBody(ErrorMessage);
        try{Messaging.sendEmail(new Messaging.SingleEmailMessage[]{emailMessage});}
        catch(exception e1){}
       
        // This is the old code that sets Contacts to delete requested
        /*try{ConList = [SELECT Id, FirstName, LastName, AVTRRT__Resume__c, QA_Review_Date__c, QA_Review__c, Title FROM Contact
                      WHERE QA_Review_Date__c = null AND QA_Review__c = :'' AND Title = :''];}
        catch(exception e1){}
        List<Contact> DelConList = new List<Contact>();
       
            //(AVTRRT__Resume__c LIKE :'%{behavior%' OR AVTRRT__Resume__c LIKE :'%!%' OR AVTRRT__Resume__c LIKE :'%urgent%'
             //OR AVTRRT__Resume__c LIKE: '%hope%' OR AVTRRT__Resume__c LIKE :'%below%' OR AVTRRT__Resume__c LIKE :'%partners%'
             //OR AVTRRT__Resume__c LIKE :'%friends%' OR AVTRRT__Resume__c LIKE :'%greetings%' OR AVTRRT__Resume__c LIKE :'%hotlist%')
       
        for(Contact C: ConList){
            if(C.AVTRRT__Resume__c != null)
            if(C.AVTRRT__Resume__c.contains('{behavior') == true || C.AVTRRT__Resume__c.contains('!') == true ||
               C.AVTRRT__Resume__c.contains('urgent') == true || C.AVTRRT__Resume__c.contains('hope') == true ||
               C.AVTRRT__Resume__c.contains('below') == true || C.AVTRRT__Resume__c.contains('partners') == true ||
               C.AVTRRT__Resume__c.contains('friends') == true || C.AVTRRT__Resume__c.contains('greetings') == true ||
               C.AVTRRT__Resume__c.contains('hotlist') == true){
                   DelConList.add(C);
               }
        }
        for(Contact C: DelConList){
            C.Delete_Request__c = true;
        }
        update DelConList;*/
    }
}
ryanjuptonryanjupton
Another stupid question. Are you triggering this from code somewhere else or setting it to run from Setup?
DannyK88DannyK88
I am setting it up from Setup. I don't call this from any trigger or other apex code.
DannyK88DannyK88
This might have not been the realy issue but when I reduced the number of contacts I queried it ran fine. I decided to do that after I looked at some debug logs. It looked like I was running over the CPU time limt. When I reduced the number of records it looked at the cpu time dropped and the program ran fine. If you see anything else that could cause the issue plesae let me know. For now I will mark this as completed.
This was selected as the best answer