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

6
  • What are you trying to accomplish? There is likely a way to do what you want, but you should be more specific.
    – sfdcfox
    Commented Oct 19, 2017 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. Commented Oct 19, 2017 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
    Commented Oct 19, 2017 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. Commented Oct 19, 2017 at 3:58
  • What does Last_Open_Timestamp_Formula__c look like?
    – sfdcfox
    Commented Oct 19, 2017 at 4:02

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .