You need to sign in to do that
Don't have an account?

Trigger on Contact to updated related Opportunity
Hi,
I'm trying to create a trigger that updates a contact's related opportunity location fields (City, State/Province, Country) based on the contact's location fields. This should only udpate the opportunity's location fields when the fields are previously blank. The code I have so far is below, but it doesn't seem to be working.
Any advice on how to get this to work is appreciated.
I'm trying to create a trigger that updates a contact's related opportunity location fields (City, State/Province, Country) based on the contact's location fields. This should only udpate the opportunity's location fields when the fields are previously blank. The code I have so far is below, but it doesn't seem to be working.
trigger updateOppLocation on Contact (after insert) { List<OpportunityContactRole> contactRoles = [SELECT OpportunityId,ContactId FROM OpportunityContactRole WHERE ContactId IN : Trigger.new]; if(contactRoles.size()>0) { Map<Id,Id> conOpp_map = new Map<Id,Id>(); for(OpportunityContactRole cr : contactRoles) { conOpp_map.put(cr.ContactId,cr.OpportunityId); } List<Opportunity> opp_list = new List<Opportunity>([SELECT Id FROM Opportunity WHERE Id IN : conOpp_map.values()]); for(Contact c : Trigger.new) { if(conOpp_map.containsKey(c.Id)) { for (Opportunity o : opp_list) { if (o.City__c == null && o.State_Province__c == null && o.Country__c == null) { o.City__c = c.MailingCity; o.State_Province__c = c.MailingState; o.Country__c = c.MailingCountry; } } } } update opp_list; } }
Any advice on how to get this to work is appreciated.
You can try below code:
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal
It seems to work very well except for one issue. When the trigger is on, the related Account location (City, State, Country) is getting overridden whenever a new lead is converted. Do you know how I could stop this from happening?
Cheers
What you can do for this is to bypass the trigger if contact is created through Lead conversion.
For this can refer below link:
https://success.salesforce.com/answers?id=90630000000hXN2AAM
Follow the process explained in above link and do below modification in your code in 8th line.
If this solves your problem, kindly mark it as the best answer.
Thanks,
Vatsal