Skip to main content
Hi,

I have written some trigger. It has nearly more custom validations made in it and there are many SOQL inside for loops. An update made in this triggers is processed and goes on excecuting which is recursive process. Any suggestions? Is there possibility to bulkify the code written?

Thanks,

Vetri
3 Antworten
  1. 11. Feb. 2015, 11:42
    Hi Vetriselvan,

    Please go through below trigger code along with comments. This will give you idea as to bulkifying the code.

    The DML operation is out of For loop.

    You can refer to best practices:

    https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code

     

    Trigger oppaccupdate on Account(after update){

    list<opportunity> opplistupdate=new list<opportunity>();

    list<opportunity> opplist=new list<opportunity>();

    for(account a:trigger.new) {

    IF (a.industry=='Agriculture') {

    opplist= [select id,stagename,accountid from opportunity where accountid=:a.id];

    }

    }

    for (opportunity opp:opplist) {

    If(opp.stagename!=null) {

    opp.stagename= 'Need Analysis';

    opplistupdate.add(opp); // Add the record to update in the list within for loop

    }

    }

    update opplistupdate; // Update the list outside the for loop i.e. any DML operation, out of for loop to avoid governor limits

    Thanks,

    Pratik

    P.S. If this answers you question, please mark it as "Best Answer" so it will help other community members too.