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
Vipin  PulinholyVipin Pulinholy 

Help to make this a bulk trigger

Hi,

 

I have this trigger and it’s not functioning for bulk update (data loader update). I get the "System.Exception: Too many SOQL queries" error.

 

I need to modify this code to work in bulk . I am new to Map & Set. Can any one please help me to modify this code to handle bulk update.

 

 trigger deleteOppContRole on Opportunity (after update) {

 

    for (Integer i = 0; i < Trigger.old.size(); i++) {
        try {


        for (Account acc : [select PersonContactId from Account where id = :trigger.new[i].AccountId])
           {
            
            OpportunityContactRole [] oDWs =

                                      [select id from OpportunityContactRole where OpportunityId = :trigger.new[i].id and ContactId=:acc.PersonContactId ];
          
     

            if (oDWs.size() > 0)
             {
             delete oDWs;
             }
           
            }
           
        } catch (System.QueryException ex) {
            //Do Nothing - There must not have been any to delete.
        }
    }
}

 

Thanks for your help.

Message Edited by das on 03-20-2009 03:15 PM
Message Edited by das on 03-20-2009 03:16 PM
Message Edited by das on 03-20-2009 03:18 PM
TSH53TSH53

How about something like this:

 

 

trigger deleteContOppRole on Opportunity (after update) { // Create a 2-dimensional list - a list of lists of Opp Contact Roles. List<List<OpportunityContactRole>> listOppContactRoleLists = new List<List<OpportunityContactRole>>(); for (Integer i = 0; i < Trigger.old.size(); i++) { try { for (Account acc : [select PersonContactId from Account where id = :trigger.new[i].AccountId]) { // Create the inner list of Opp Contact Roles to Delete List<OpportunityContactRole> listOppContactRolesToDelete = new List<OpportunityContactRole>([select id from OpportunityContactRole where OpportunityId = :trigger.new[i].id and ContactId=:acc.PersonContactId ]); // If there are contact roles to delete, add the inner list to the outer list. if (listOppContactRolesToDelete.size() > 0) { listOppContactRoleLists.add(listOppContactRolesToDelete); } } } catch (System.QueryException ex) { //Do Nothing - There must not have been any to delete. } } // Iterate the outer list and delete the contents of the inner list. for (Integer i = 0; i < listOppContactRoleLists.size(); i++) { delete listOppContactRoleLists[i]; } }

 

Vipin  PulinholyVipin Pulinholy

Thaks much for your help. But no luck. I still ge the error ":System.Exception: Too many SOQL queries: "

:-(

 

I came up with anther version....but still the same error. here is the new version of code. I think we will be able fix this code if can remove the SQL statement from for loop...Not sure How to do that....Any help???

 

 

trigger deleteOppContRole on Opportunity (after update) {


List< OpportunityContactRole > aList = new List< OpportunityContactRole >();

for ( Opportunity o : Trigger.new ) {

Account acc = [select PersonContactId from Account where id = :o.AccountId];

OpportunityContactRole ocr = [select id from OpportunityContactRole where OpportunityId = :o.id and ContactId=:acc.PersonContactId];

aList.add( ocr );




}
System.debug('I am here: 1');

delete aList;

Message Edited by das on 03-27-2009 05:22 PM