+ Start a Discussion
sakthidasan salesfrocesakthidasan salesfroce 

batch class from trigger

I'm new to salesforce Please explain these question
Is it advisable to call batch class from trigger? If Yes, Why? If No, Why?
Best Answer chosen by sakthidasan salesfroce
Mahesh DMahesh D
Hi Sakthidasan,

A batch apex can be called from a class as well as from trigger code. But, we have to be very very carefull while calling a batch apex from trigger.

In your Trigger code something like below :-
 
// BatchClass is the name of batchclass
BatchClass bh = new BatchClass();
Database.executeBacth(bh);

My only concern is that the Trigger would fire on the DML events and that would invoke the Batch.Do you want to run batch as many times as the Trigger would fire ?? Then you can proceed, but my personal openion is not to go with it.


Please follow the below links on the same post:

http://sfdcsrini.blogspot.com/2014/06/how-to-execute-batch-apex-using-apex.html

https://developer.salesforce.com/forums/?id=906F0000000AarkIAC

https://developer.salesforce.com/forums/?id=906F00000008zgyIAA

http://salesforce.stackexchange.com/questions/92047/call-schedulable-batch-class-from-trigger-instead-of-any-schedule

http://www.cloudforce4u.com/2013/11/call-batch-apex-from-trigger.html

http://theblogreaders.com/how-to-execute-batch-apex-using-apex-trigger/#.Vuo5DOYohv5

http://www.infallibletechie.com/2012/05/how-to-run-batch-apex.html



Batch Apex Best Practices


http://sfdcrocket.blogspot.com/2013/12/batch-apex-best-practices.html

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi Sakthidasan,

A batch apex can be called from a class as well as from trigger code. But, we have to be very very carefull while calling a batch apex from trigger.

In your Trigger code something like below :-
 
// BatchClass is the name of batchclass
BatchClass bh = new BatchClass();
Database.executeBacth(bh);

My only concern is that the Trigger would fire on the DML events and that would invoke the Batch.Do you want to run batch as many times as the Trigger would fire ?? Then you can proceed, but my personal openion is not to go with it.


Please follow the below links on the same post:

http://sfdcsrini.blogspot.com/2014/06/how-to-execute-batch-apex-using-apex.html

https://developer.salesforce.com/forums/?id=906F0000000AarkIAC

https://developer.salesforce.com/forums/?id=906F00000008zgyIAA

http://salesforce.stackexchange.com/questions/92047/call-schedulable-batch-class-from-trigger-instead-of-any-schedule

http://www.cloudforce4u.com/2013/11/call-batch-apex-from-trigger.html

http://theblogreaders.com/how-to-execute-batch-apex-using-apex-trigger/#.Vuo5DOYohv5

http://www.infallibletechie.com/2012/05/how-to-run-batch-apex.html



Batch Apex Best Practices


http://sfdcrocket.blogspot.com/2013/12/batch-apex-best-practices.html

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
Allen li 3Allen li 3
Hi Sakthidasan,

Salesforce batch is about processing large volume data and batch resources. And batch is very restricted resources in salesforce. Obviously, you should not call a batch class from a trigger in any situation even though you can do this in your code.

FYI, you can try a new salesforce development IDE named Metaforce on chrome application platform. it's a great tool for coding, deploying, etc.. you can try to search and install it in chrome webstore or click this link.(https://chrome.google.com/webstore/detail/metaforce/hhnkaakhlhngcdckdiogpkjihnmgodep)
User-added image