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
Ryan MeyerRyan Meyer 

Scheduled apex job to post lead count keeps posting the same number??

Hello, 

I have a simple apex class that looks up unassigned leads (the # currently in the default queue) and posts the current count to Slack via webhook. The class works fine, however, I scheduled it to run twice per day and it keeps posting the same number each time -- what I believe is the number of leads that were in the queue at the time of scheduling... not at the time it runs.

I'm not sure if I'm completely missing something here or what, but I don't see how/why that would be the case. Also... how do I get around this? Create a class to schedule which simply calls the other class? Code below...

Thank you! 
 
public class PostLeadCounttoSlack implements schedulable { 

    private static final String slackURL = 'https://hooks.slack.com/services/T0xxxxxxxxxxxxxxxxxxxxxxx';
    AggregateResult res = [SELECT count(id) From Lead where OwnerId in (SELECT Id from Group where type='Queue' and Name='Default Lead Queue')];
    Integer lc = integer.valueof(res.get('expr0'));

    public void execute(SchedulableContext sc){

    Map<String,Object> msg = new Map<String,Object>();
        msg.put('text', 'There are ' + lc + ' leads in the queue.');
        msg.put('mrkdwn', true);
        msg.put('link_names', '1');
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
    }

    public class QueueableSlackCall implements System.Queueable, Database.AllowsCallouts {
         
        private final String url;
        private final String method;
        private final String body;
         
        public QueueableSlackCall(String url, String method, String body) {
            this.url = url;
            this.method = method;
            this.body = body;
        }
         
        public void execute(System.QueueableContext ctx) {
            HttpRequest req = new HttpRequest();
            req.setEndpoint(url);
            req.setMethod(method);
            req.setBody(body);
            Http http = new Http();
            HttpResponse res = http.send(req);
        }
 
    }    
}
Rohit Kumar SainiRohit Kumar Saini
Hi Ryan,

It depends on where below two lines are situated:
AggregateResult res = [SELECT count(id) From Lead where OwnerId in (SELECT Id from Group where type='Queue' and Name='Default Lead Queue')];
    Integer lc = integer.valueof(res.get('expr0'));

If you move these two lines in execute method, it will calculate count when schedule job executes. Final code will look like this:
 
public void execute(SchedulableContext sc){
AggregateResult res = [SELECT count(id) From Lead where OwnerId in (SELECT Id from Group where type='Queue' and Name='Default Lead Queue')];
    Integer lc = integer.valueof(res.get('expr0'));

Map<String,Object> msg = new Map<String,Object>();
        msg.put('text', 'There are ' + lc + ' leads in the queue.');
        msg.put('mrkdwn', true);
        msg.put('link_names', '1');
        String body = JSON.serialize(msg);    
        System.enqueueJob(new QueueableSlackCall(slackURL, 'POST', body));
}

This code actually enqueue Queueable job which can run later. If you want it to calculate when Queueable job runs, you need to put calculation and msg creation logic in execute method of Queueable job before http callout. Please let me know for any issues.

Thanks.
Ryan MeyerRyan Meyer
Ah that makes sense, thank you very much! Ryan