You need to sign in to do that
Don't have an account?
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.
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]; } }
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;