You need to sign in to do that
Don't have an account?
EtienneCoutant
Batch Apex in triggers?
Hi,
I was reading in the Salesforce Summer '09 Force.com Apex Code Developer's Guide that we will be able to use Batch Apex to build complex processes, like data cleansing...
Will it be possible to start a Batch job from a trigger? If yes, is there any limitations due to governors?
Thanks,
Etienne
The Apex docs have been updated and you can read all about how to fire a Batch apex job (Database.executeBatch(...)) and their governorlimits at :
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
David
yes you can queue you job by using time dependend workflow.
Suppose you want to clean you lead object data. So you can implment time dependent workflow on lead object which will internally update some field on lead and execute your batch apex code. I hope this will help!
Sorry I'm not able to understand your statement of Queing Job using Time Dependent workflows ...
The Batch APEX I created works fine with Single Record Edit/Delete but with ultiple Records i.e. when used with Dataloader it breaks down. With Time trigger may be i can put them on hold for some time but they still gonna fire at same time when time reaches the trigger?
I was hoping some kind of solution to Create a JOB Queue itself from inside my trigger such that the Batch jobs execute in a Queing Order thus prevented to be executed in parallel ... Can this be done ?
For Single record it will work because no of job is < 5.
For bulk update, before adding new job in queue first check number of existing job in AsyncApexJob. If it is more than 5 then add new record in some other object(e.g. batchMonitorObject) and set time dependent workflow on that object which internally add Apex Job after some time. This way you can avoid number of apex job restriction.Don't forget to check number of existing job each time before adding new job in queue.
Here's the code for the specific 3rd party app, Financialforce:
http://code.google.com/p/ffdcapisamples/source/browse/ffdcapisamples/src/classes/SimpleJournalImportBatch.cls
And here's the sample code to use it
trigger SimpleJournalTrigger on SimpleJournal__c (after insert)
{
// Build a list of ID's
List<ID> ids = new List<ID>();
for(SimpleJournal__c simpleJournal : Trigger.new)
ids.add(simpleJournal.Id);
// Start Batch Apex job to create Journals
Database.executeBatch(new SimpleJournalImportBatch(ids));
}
Also Apex Scheduler sample, haven't looked at it yet:
http://code.google.com/p/ffdcapisamples/source/browse/ffdcapisamples/src/classes/SimpleJournalImportScheduler.cls
Also see page 146 of Apex Reference manual http://www.salesforce.com/us/developer/docs/apexcode/salesforce_apex_language_reference.pdf
Oh right. Data loader is still a sticking point since you can only have 5 jobs queued.
What if you loaded them all in data loader then had a separate web services call to an Apex Class to initiate the processing instead of a trigger? Not sure if that would bypass the governor limits in question, but it may chunk more successfully since it's being initiated from w/in an Apex Class instead of trying to queue up a bunch of triggers?