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
EtienneCoutantEtienneCoutant 

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

David VPDavid VP

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

VarunCVarunC
Well I don't think Batch APEX can be used inside a trigger :( ... My Batch APEX always breaks down when I update records from Dataloader. It breaks down with an error message stating that No more than 5 concurrent executions of Batch Job Allowed ..  I'm still looking to get past this error, but unable to do so .. I was hoping there can be a way to serialize the Batch jobs such that Jobs gets Queued instead of Exected in parallel ...
NaishadhNaishadh
you can invoke batch apex from a trigger but you must use extreme care. SalesForce allows only 5 batch job.
VarunCVarunC
Yes ... :) .. but I don't know What Extreme care i can take :) ... I mean I want to Queue the jobs, Can I do that ? Is there any way or practice to take care in the trigger when executing my job ?
NaishadhNaishadh

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!


VarunCVarunC

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 ?

NaishadhNaishadh

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.

VarunCVarunC
Yes .. this looks promising Idea :D .. Thanks ... Though this seems to be hell of work :( .. I need to make sue the State of Trigger records also gets stored correctly and it would get more complex with My need to Use Batch APEX in Multiple triggers :) ... Anyways thanks a bunch for the head start here ....
bvdbbvdb
vchaddha, any luck with this?
VarunCVarunC
No ... not yet ...  :(
bvdbbvdb
I'm waiting on some Batch Apex sample code from the developer of a managed package, supposedly by end of the week. Will update here if useful/applicable.
VarunCVarunC
add me to your queue for updates on this :)
bvdbbvdb

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

VarunCVarunC
Thanks ... were you able to get any info on Bulk Update issue, i mean if I update or insert 5000 records from Data loader, was the original developer able to perform that successfully? .. because my attempt on my batch apex class broke down with records greater than 1000 :( ..
bvdbbvdb

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?

VarunCVarunC
I suspect we have CallOuts limited too when invoked from triggers ...
bvdbbvdb
No way to identify "batch has been uploaded" and then initiate the processing?
VarunCVarunC
yes there is an ApexJobs object which we can query and read current job's status and then if there are currently 5 jobs created then we can restrict creation of more jobs ..