You need to sign in to do that
Don't have an account?
Brian Ford
Before Trigger Not Updating Record
I have a before update trigger on the opportunity. I would like to count the number of contact roles on each opportunity in the trigger as well as indicate on the opportunity if at least one of the contact roles is flagged as a primary contact.
My original solution used a SOQL for loop inside a for loop and was running through my governor limits:
So I've created this work around. The problem is that the last 2 lines that should update 2 fields on the opportunity aren't updating the opportunity. In my debug log it shows that the fields are being updated but those changes aren't being written to the database. I'm sure there's a simple solution here that I'm not seeing. Thanks for your help.
My original solution used a SOQL for loop inside a for loop and was running through my governor limits:
trigger ContactRole on Opportunity (before update) { for (Opportunity o : trigger.new) { Integer count = 0; Boolean hasPrimary = FALSE; for (OpportunityContactRole ocr : [SELECT Id, isPrimary FROM OpportunityContactRole WHERE OpportunityId =:o.Id]) { count++; if (ocr.isPrimary) hasPrimary = TRUE; } o.Number_of_Contact_Roles__c = count; o.Has_Primary_Contact__c = hasPrimary; } }
So I've created this work around. The problem is that the last 2 lines that should update 2 fields on the opportunity aren't updating the opportunity. In my debug log it shows that the fields are being updated but those changes aren't being written to the database. I'm sure there's a simple solution here that I'm not seeing. Thanks for your help.
for (Opportunity o : [SELECT Id, Has_Primary_Contact__c, Number_of_Contact_Roles__c, (SELECT Id, isPrimary FROM OpportunityContactRoles) FROM Opportunity WHERE Id IN :trigger.newMap.keyset()]) { Integer count = 0; Boolean hasPrimary = False; for (OpportunityContactRole ocr : o.OpportunityContactRoles) { count++; if (ocr.isPrimary) hasPrimary = TRUE; } o.Number_of_Contact_Roles__c = count; o.Has_Primary_Contact__c = hasPrimary; System.debug('opportunity: ' + o); }
All Answers
Kindly mark this questiona as Solved if this fixed your issue