You need to sign in to do that
Don't have an account?

CRONKit-Scheduler
Hello All,
Is anyone familiar with this program. The Set Up Guide provided an apex trigger code as an example however I continue to receive a warning message everytime I save this trigger. Is there something wrong with the code?
Is anyone familiar with this program. The Set Up Guide provided an apex trigger code as an example however I continue to receive a warning message everytime I save this trigger. Is there something wrong with the code?
![]() | Error: Compile Error: unexpected token: at line 1 column 9 |
trigger batchRun on cron__Batch_Run__c (before insert, before update) {
// This Apex trigger is designed to fire when the batch workflow scheduler
// checks the Trigger Batch Run checkbox or when changes are made to the Batch Run
// record manually.
Boolean error = false; // Var used by each batch job to flag and return an error to the Batch Run object.
String results = ''; // Batch job results, also returned to the Batch Run object.
for (cron__Batch_Run__c batchRun : Trigger.new) {
System.debug(batchRun);
if ( batchRun.cron__Completed__c != null) {
System.debug('Job is already completed');
continue; // Job has alread run, skip all this
}
if ( batchRun.cron__Trigger_Batch_Run__c == true ) {
System.debug('Trigger Batch Run set. Running batch job.');
// --------------- Batch Job Housekeeping --------------------
Datetime lastrun = Datetime.now();
Datetime nextrun;
if(batchRun.cron__Run_Every_Units__c == 'Days') {
nextrun = lastrun.addDays(batchRun.cron__Run_Every__c.intValue());
} else {
nextrun = lastrun.addHours(batchRun.cron__Run_Every__c.intValue());
}
if (nextrun < Datetime.now()) {
nextrun = Datetime.now();
}
// Create the next Batch Run and configure it so that the scheduler workflow
// adds a Trigger_Batch_Run field update in the time-based workflow queue.
cron__Batch_Run__c newJob = new cron__Batch_Run__c(
cron__Scheduled_To_Run__c = nextrun,
cron__Trigger_Batch_Run__c = false,
cron__Batch_Job_Name__c = batchRun.cron__Batch_Job_Name__c,
cron__Batch_Job__c = batchRun.cron__Batch_Job__c,
cron__Run_Every__c = batchRun.cron__Run_Every__c,
cron__Run_Every_Units__c = batchRun.cron__Run_Every_Units__c,
cron__Trigger_Scheduler_1__c = true);
insert newJob;
// Update the current Batch Run dates and uncheck batch job trigger
batchRun.cron__Completed__c = lastrun;
if (batchRun.cron__Scheduled_To_Run__c == null) {
batchRun.cron__Scheduled_To_Run__c = lastrun;
}
batchRun.cron__Trigger_Batch_Run__c = false;
// ------------ End Batch Job Housekeeping -------------------
// ----------- Begin batch jobs -----------------
if (batchRun.cron__Batch_Job_Name__c == 'Sample Batch Job') {
error = false;
results = '';
// Insert your Apex code here... be sure to set vars 'error' and 'results' to
// pass batch results back to the Batch Run object.
}
// ----------- End batch jobs -----------------
// Report Governor Limit Stats and set return values
String limitText = 'Aggregate Queries: '+
Limits.getAggregateQueries() +'/' +
Limits.getLimitAggregateQueries();
limitText += '\nSOQL Queries: '+
Limits.getQueries() +'/' +
Limits.getLimitQueries();
limitText += '\nQuery Rows: '+
Limits.getQueryRows() +'/' +
Limits.getLimitQueryRows();
limitText += '\nDML Statements: '+
Limits.getDMLStatements() +'/' +
Limits.getLimitDMLStatements();
System.debug(limitText);
batchRun.cron__Results__c = results;
batchRun.cron__Results__c += '\n\n'+limitText;
if (error) {
// write error to batch run notes field and set error flag
batchRun.cron__Result__c = 'Error';
} else {
batchRun.cron__Result__c = 'Success';
}
} else { // end if trigger batch job flag set
System.debug('Refreshing time-based workflow queue');
// Alternate Trigger Scheduler flags to keep workflow queued and current
if (batchRun.cron__Trigger_Scheduler_1__c == false) {
batchRun.cron__Trigger_Scheduler_1__c = true;
batchRun.cron__Trigger_Scheduler_2__c = false;
} else {
batchRun.cron__Trigger_Scheduler_1__c = false;
batchRun.cron__Trigger_Scheduler_2__c = true;
}
}
}
}
You might have special characters in the source that aren't visible when you paste the code. To clean it up you might try pasting it into a text editor first and then copying it to the IDE.
If you can't get that to work, let me know and I'll send you a clean copy of the trigger code.
It strikes me as odd that I'd have to edit the object's properties to add new cron entries. I thought that those would be done through the Batch Run tab or something. Although I did find an option to set the Batch Job Type to Custom but couldn't find anywhere that allows me to set an apex class/code to run.
Maybe I've just got the wrong impression of what this package does.
Any help is much appreciated.
The Batch_Run object is simply used to control execution of the batch job and to store the results of previous batch runs. You set the interval in the "standard" fields on the Batch_Job record and the time-based workflow and basic code in the trigger template we provide takes care of the rest.
Keep in mind that this is native batch scheduling, so your batch triggers need to be aware of governor limits and handle them as they make sense in the context of your batch job. The built-in cleanup scripts with CronKit remove as many records as possible, and then come back to finish the job later if governor limits prevent the script from removing all of the records in one shot.
As far as being able to select an Apex class from the Batch Run record, that would require being able to execute anonymous Apex from a trigger - which Salesforce doesn't allow at this point. CronKit is a work-around :-)
Hope that helps.
Message Edited by Ron Wild on 10-19-2008 12:18 PM
Hi Ron
Could you please send me the latest copy of the Cronkit trigger? The version on my Salesforce is managed which means I can not view or edit the code. I tried creating another trigger but it does not appear to fire.
Could you please send the copy of the trigger to smoore10000@gmail.com?
Thanks
Stephen
Stephen,
Documentation, sample code, instructions, etc. are all available here http://www.linvio.com/cronkit.php
Regards,
Ron
Hi Ron
How do I deleted or deactivate this existing Cronbatchtrigger from the Batch_Run object?
Thanks
Stephen
Your batch triggers only execute if you set the batch job type to a specific value - so they should co-exist. However, I think there's a checkbox on the settings page that allows you to disable the built-in trigger.
Please check the documentation or go to the CronKit forum for more information. www.linvio.com/forum.
R