I have this Schedulable Class that finds all Email Campaigns of a certain age and sends that age to an external service. I am pretty sure I am doing it wrong. First I want to pull the values for the integer array from a custom label.email_campaign_ages, but cant seem to make that happen, so I hard coded the values in.
global class SGsegUPDT implements schedulable {
// set cron for everyday at 10pm
public static String CRON_EXP = '0 0 10 1/1 * ? *';
// Days to send emails
String label = system.label.email_campaign_ages;
public Static Integer[] ecAges = new Integer[]{0, 3, 22, 52, 112, 202, 382};
// made it this way to only sendRequest once instead of once per age group
global static List<Map<String, Object>> jsonMAP = new List<Map<String, Object>>();
// not sure if this is needed.
global static String scheduleIt() {
SGsegUPDT segUP = new SGsegUPDT();
return System.schedule('Sendgrid Daily update', CRON_EXP, segUP);
}
global void execute(SchedulableContext ctx) {
//get all leads
for (Integer i : ecAges) { System.debug(i);
for (email_campaign__c ec : [
SELECT email__c, First_Name__c, Last_Name__c , Campaign_Age__c , Agency_Type__c
FROM email_campaign__c
WHERE Campaign_Age__c = :i]) {
Map<String, Object> mapped = new Map<String, Object>{ //they are flipped at serailization
'type' => ec.Agency_Type__c, //to come out correct listed in reverse
'age' => ec.Campaign_Age__c,
'last_name' => ec.Last_Name__c,
'first_name' => ec.First_Name__c,
'email' => ec.Email__c
};
jsonMAP.add(mapped); system.debug(mapped);
}
}
String jsonString = JSON.serialize(jsonMAP);
//send the jsonpayload, endpoint uri, and verb
SendGridAPI.sendRequest(jsonString,'/v3/contactdb/recipients', 'PATCH'); system.debug(jsonString);
}
}
I feel like I am going to get some limit errors, should this be done in batch instead? I know its working on a small scale as is. Just not sure when its running thousands or records a night what will happen...