1

What is the best possible way to check record that match with my criteria and update it? i have try using scheduled jobs that run every 15 minutes but sometimes it run sometimes it doesn't run. it seems it will run depend on available resource

Here scheduled apex code

global class CallQueueLeadChecker implements Schedulable {
Datetime dtNow = system.now();

global void execute(SchedulableContext SC){
checkLead();
}

public void checkLead(){
    List<Lead> leadList = [SELECT Id,Active_Call_Queue2__c  FROM Lead WHERE Active_Call_Queue2__c = FALSE AND No_Call_Queue__c = FALSE AND (NOT Phone LIKE '%wrong%') AND (NOT Phone LIKE '%invalid%') AND (NOT MobilePhone LIKE '%wrong%') AND (NOT MobilePhone LIKE '%invalid%') AND (Phone !=null OR MobilePhone !=null) AND (Status='Open' OR Status = 'Contacted') AND Picked_By__c=null AND Is_Picked__c = FALSE AND (Last_Open_Timestamp_Formula__c <: dtNow OR Last_Open_Timestamp_Formula__c = NULL) ORDER BY Hubspot_Score__c DESC NULLS LAST LIMIT 200];
    for(Lead l : leadList){
        l.Active_Call_Queue2__c = TRUE;
    }
    update leadList;
  }
}

These are the cron jobs to run schedule apex every 15 minutes

System.schedule('Call Queue Checker 0', '0 0 * * * ?', new CallQueueLeadChecker());
System.schedule('Call Queue Checker 15', '0 15 * * * ?', new CallQueueLeadChecker());
System.schedule('Call Queue Checker 30', '0 30 * * * ?', new CallQueueLeadChecker());
System.schedule('Call Queue Checker 45', '0 45 * * * ?', new CallQueueLeadChecker());

Regards, Brian

  • What are you trying to accomplish? There is likely a way to do what you want, but you should be more specific. – sfdcfox Oct 19 '17 at 3:27
  • Hi @sfdcfox, thanks for reminding me. I have edited the post, i would like to run code to check all of the lead that match with query criteria then update the value of Active_Call_Queue2__c field. – febriantarigan Oct 19 '17 at 3:38
  • Based on your question, you would probably be better served setting up a Time-Based Workflow Rule or Process Builder action. Basically, set the criteria to be the filters you want, the time-based delay to be the value of the formula, with an action to update the field. You'll still have accuracy to about 15 minutes, but without any code. – sfdcfox Oct 19 '17 at 3:45
  • if I used time-based or process builder, it won't check if there is no trigger in that record right? because Last_Open_Timestamp_Formula__c is formula field that will change without triggering the record. – febriantarigan Oct 19 '17 at 3:58
  • What does Last_Open_Timestamp_Formula__c look like? – sfdcfox Oct 19 '17 at 4:02
3

There is no way to schedule anything on the dot, scheduled job is the closest you can get, but as you said it won't be guaranteed that it'll run exactly on the 15th minute.

but sometimes it run sometimes it doesn't run.

  • It will always run, but not necessarily at the scheduled time, a little bit later.

If it's time critical, you might need to run a scheduled job on an external server where it's guaranteed it'll run there and then, and perform the check/update the record in Salesforce via the API.

  • Hi @BorisBachovski, Do you have any reference about run scheduled job on an external server? Thanks – febriantarigan Oct 23 '17 at 7:47
  • If it's a Linux server just look up "cron job" – Boris Bachovski Oct 23 '17 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.