+ Start a Discussion
Atla MasthanaiahAtla Masthanaiah 

After Insert Triggers

trigger coin on Contact (before insert) {
    if(trigger.isinsert ){
        Integer recordCount = Trigger.New.size();
  BtachapexEmail b=new  BtachapexEmail();
        database.executebatch(b);
    }
}
Nagendra Prasad ChNagendra Prasad Ch
Hi Atla,
What is the issue you are facing in this code?

This trigger fires Before Insert. If you want it to trigger After insert you have to change the first line to
trigger coin on Contact (After insert) {
Is this u want?
Mahesh DMahesh D
Hi Atla,

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.
 
// 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