You need to sign in to do that
Don't have an account?
Ryan 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);
}
}
}
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);
}
}
}
It depends on where below two lines are situated:
If you move these two lines in execute method, it will calculate count when schedule job executes. Final code will look like this:
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.