You need to sign in to do that
Don't have an account?
mgodsey
Help bulkifying a trigger
I have a trigger that is working the way I want it to, but it's not bulkified. If someone could help me figure out how to move the SOQL query out of the for loop I would really appreciate it! This trigger is on the OpportunityTeamMember object. When one team member is added, a field on the Opp should update to "Yes" and if a team member is deleted and there will not be any remaining team members after deletion, the field on the Opp should update to "No."
//------------------------------------------Before Delete---------------------------------------- if(Trigger.isBefore && Trigger.isDelete){ //create collections to store 1) IDs for Opportunities for deleted team member(s), 2) Actual Opportunity records with deleted Team Members, 3) Opps that will need to be updatead to say "No" not a Shared Opportunity Set<Id> oppIDs = new Set<ID>(); List<Opportunity> oppsWithDeletedTeamMembers = new List<Opportunity>(); List<Opportunity> oppsToUpdate = new List<Opportunity>(); //loop through deleted team members and add their Opportunities to the list above for (OpportunityTeamMember deletedTM : trigger.old){ oppIDS.add(deletedTM.OpportunityID); system.debug('[MF Debug] Opportunity field from deleted Team Member = ' + deletedTM.OpportunityID); } oppsWithDeletedTeamMembers = [SELECT Id FROM Opportunity WHERE id =: oppIDs]; system.debug('[MF DEBUG] Opps with Deleted Team Member = ' + oppsWithDeletedTeamMembers); /* *Loop through the Opportunities in the OppsWithDeleteTeamMembers list and see how many team members pre-deletion. *If it's less than or equal to 1, the Opp will no longer be considered shared after that team member is deleted. */ for (Opportunity opp : oppsWithDeletedTeamMembers){ List<OpportunityTeamMember> currentTeamMembers = [SELECT ID FROM OpportunityTeamMember WHERE OpportunityID =: opp.ID]; Integer currentTeamMemberCount = currentTeamMembers.size(); system.debug('[MF DEBUG] currentTeamMemberCount = :' + currentTeamMemberCount); if(currentTeamMemberCount <= 1){ opp.SharedOpportunity__c = 'NO'; oppsToUpdate.add(opp); } } update OppsToUpdate; }
Here is the trigger code and apexclass for opportunityteammember bulk delete, update and insert
Apex class for trigger
triger on opportunityteammember
All Answers
Here is the trigger code and apexclass for opportunityteammember bulk delete, update and insert
Apex class for trigger
triger on opportunityteammember
Thank you for your help. I understand most of the class, but I'm unsure as to why we are creating the tempTeamMems list. Do you mind explaining why that is included? Thank you so much!
Let say one opporunity has three opportunityTeamMember records.
for loop wiil execute each time for each opportunityTeamMember record.
step 1: First oppteammember record enters into for loop, if condition checks the opportunityId of team member is aleredy added to map key value or not. Because of first oppteam record the map does not contain key- value an opportunityId of team record. so both opportunityId and team record will added to map . in this step list in map contains only one team record.
When the second team record of same opportunity enter into the for loop,it will check first the map containd this opportunityid or not. If map already contains opportunity Id means , we will collect already added team record into a list add the new one we will add to that list and the total list added to map. In this step list in map contains two team records.
Similery for third record.
Add this line in your code below
tempTeamMems.add(tm);
oppToOppTeamMems.put(tm.oppotunityId, tempTeamMems);
Thank you so much for your help. I had to make a few tweaks, but now it's working when I test in bulk in my unit test!