You need to sign in to do that
Don't have an account?
Dave Scott
Help with trigger
Hi folks,
I have a trigger that updates a custom field on the Opportunity called 'Primary Contact' with the name of the Primary Contact on the related Opportunity when the Stage is changed to either '8-Deal Lost' or '9-Deal Withdrawn'.
Please can you help me make changes to this trigger that would carry out this update whenever the Opportunity record is updated rather than when the Stage Name is chanaged?
Hope that's ok, many thanks in advance for all your help.
Many thanks,
Dave
I have a trigger that updates a custom field on the Opportunity called 'Primary Contact' with the name of the Primary Contact on the related Opportunity when the Stage is changed to either '8-Deal Lost' or '9-Deal Withdrawn'.
Please can you help me make changes to this trigger that would carry out this update whenever the Opportunity record is updated rather than when the Stage Name is chanaged?
trigger trgr_Opportunity_FetchPrimaryContactEmail on Opportunity ( after Insert, after Update ) { Set setOpportunity = new Set(); for(Opportunity oppty : trigger.new){ if( oppty.StageName == '8-Deal Lost' || oppty.StageName == '9-Deal Withdrawn' ){ setOpportunity.add(oppty.Id); } } if(!setOpportunity.isEmpty()){ Map opptyToOpportunityContactRoleMap = new Map(); for( OpportunityContactRole conRole: [ SELECT Id, OpportunityId, ContactId FROM OpportunityContactRole WHERE OpportunityId IN :setOpportunity AND IsPrimary = TRUE ] ){ opptyToOpportunityContactRoleMap.put( conRole.OpportunityId, conRole ); } List lstOpportunityToUpdate = new List(); if(!opptyToOpportunityContactRoleMap.isEmpty()){ for(Opportunity oppty : trigger.new){ if( opptyToOpportunityContactRoleMap.containsKey(oppty.Id) && oppty.Primary_Contact__c != opptyToOpportunityContactRoleMap .get(oppty.Id) .ContactId ){ lstOpportunityToUpdate.add( new Opportunity( Id = oppty.Id, Primary_Contact__c = opptyToOpportunityContactRoleMap .get(oppty.Id) .ContactId ) ); } } if(!lstOpportunityToUpdate.isEmpty()){ UPDATE lstOpportunityToUpdate; } } } }
Hope that's ok, many thanks in advance for all your help.
Many thanks,
Dave
for(Opportunity oppty : trigger.new)
{
setOpportunity.add(oppty.Id);
}
All Answers
You can remove line 9-14. Please check the code below.
Thanks,
Himanshu
Please mark this as solution if this will help. So that some one has same issue this post can help
Thanks,
Amit Chaudhary
I tried to remove lines 9-14, however when deploying I get an error stating:
Any ideas?
Many thanks,
Dave
I tried to use your code, however it wouldn't let me save it and gave me the error message: This is the code I used: Many thanks,
Dave
for(Opportunity oppty : trigger.new)
{
setOpportunity.add(oppty.Id);
}