Skip to content

Instantly share code, notes, and snippets.

@kevinohara80
Last active December 16, 2015 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinohara80/5467922 to your computer and use it in GitHub Desktop.
Save kevinohara80/5467922 to your computer and use it in GitHub Desktop.
Schedulable batch trigger invoke class
global class CampaignMemberTouch implements Schedulable{
global void execute(SchedulableContext SC) {
// kick off the batch with a size of 2 for the batch size
InvokeTriggerBatch.invoke('SELECT Id FROM CampaignMember', 5);
}
}
  1. Install all of the classes here
  2. Adjust the tests if you need to.
  3. Deploy to production
  4. Use the apex schedule to schedule the CampaignMemberTouch job.
global class InvokeUpdateTriggerBatch implements Database.Batchable<sObject> {
private String query;
global InvokeUpdateTriggerBatch(String query) {
this.query = query;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(this.query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
update scope;
}
global void finish(Database.BatchableContext BC){
System.debug('Batch Process Complete');
}
// utility static method to kick off a batch in one line.
global static String invoke(String query, Integer batchSize) {
InvokeUpdateTriggerBatch batch = new InvokeUpdateTriggerBatch(query);
return Database.executeBatch(batch, batchSize);
}
}
@isTest
private class InvokeUpdateTriggerBatch_Test {
static testmethod void testBatch() {
List<Lead> leads = new List<Lead>();
for(Integer i=0; i<100; i++) {
// adjust this test if you have additional validation
// rules or other things that might make this fail.
Lead l = new Lead();
l.FirstName = 'Test' + String.valueof(i);
l.LastName = 'Test' + String.valueof(i);
l.Email = 'test' + String.valueof(i) + '@test.com';
l.Company = 'Blah Company';
leads.add(l);
}
insert leads;
Test.startTest();
String batchId = InvokeUpdateTriggerBatch.invoke('SELECT Id FROM Lead');
Test.stopTest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment