0

I have this trigger which is not bulk. How can I make it bulk?

trigger DeleteOpportunity on Order__c(before delete){ 

    if(trigger.IsDelete) {

        List<Opportunity> lstOppDelete = new List<Opportunity>();

        for(Order__c orderd : Trigger.old) { 

            Opportunity opt=[Select Id,Orders__c From Opportunity Where Orders__c=:orderd.id LIMIT 
            1];  
            lstOppDelete.add(opt); 
        }  

        delete lstOppDelete;
    }
}
6

The best way to bulkify Apex code is to use sets and maps. This example will fetch all Opportunities associated with the deleted orders and delete them. Please note you could still exceed limits for rows returned if too many Opportunities are related to your deleted record.

trigger DeleteOpportunity on Order__c(before delete){ 

    if(trigger.IsDelete) {
            List<Opportunity> opt=[Select Id 
                                  From Opportunity 
                                  Where Orders__c IN :trigger.old];  
            delete opt;    
    }  
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy