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
Catco CRMCatco CRM 

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?
ErrorError: 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;
}

}

}
}

Ron WildRon Wild
Try creating the trigger from scratch  (e.g. "New Apex Trigger" > enter a name > select the object > check off the trigger types ...) and see if you get an error with just the shell.

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.


phyerlightphyerlight
Hi, I have a question that hopefully can be answered here since I don't seem to be getting a response on the Linvio forums. Based on the first post, it seems like the code is added to a trigger in the 'Batch Run' cronkit object. Does all this code have to be added for each cron entry that is to be run? Is this the intended way to run arbitrary apex code at a certain interval? How do you set the interval?

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.
Ron WildRon Wild
CronKit is just the scheduling framework for whatever batch work you have to do.   Since we can't predict what the apex code will do, or what parameters you need to pass to the code, we've simply allowed you to extend the batch job object with your own batch job type (by adding a unique name to the batch job type picklist you mentioned in your post) and with custom fields that can be used to hold the parameters for your batch job.   If you abstract your batch code so that parameters can be set to have it do different jobs, or to work on different objects, then you only need one trigger.  Then multiple batch jobs can be set up to run the same code ... each with a separate set of parameters.

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
TraceyTracey

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

Ron WildRon Wild

Stephen,

 

Documentation, sample code, instructions, etc. are all available here http://www.linvio.com/cronkit.php

 

Regards,

Ron

TraceyTracey

Hi Ron

 

How do I deleted or deactivate this existing Cronbatchtrigger from the Batch_Run object?

 

 

Thanks

 

Stephen

Ron WildRon Wild

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