Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Trigger on Contact to update opportunity
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 11:04 AM
Hi Everyone,
I'm writing a trigger to update opportunity When Contact is updated & Email or Fax fields are changed, update the Email & Fax from Contact into the Opportunity.If possible provide some sample code how to achieve this.
Re: Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 12:36 AM
Hi,
Please try below :-
I created a lookup relationship on Contact called Opportuniyt__c which is lookup to opportunity.
trigger updateRelatedOpp on Contact (before insert,before update) {
List<Contact> conList = new List<Contact>();
List<Id> idList = new List<Id>();
List<Opportunity> oppList = new List<Opportunity>();
for(Contact newCon : Trigger.new){
Contact oldCon = Trigger.oldMap.get(newCon.Id);
if(oldCon.Email!=newCon.Email || oldCon.Fax!=newCon.Fax){
idList.add(newCon.Opportunity__c);
}
}
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([select id,Email__c,Fax__c from Opportunity where id in:idList]);
for(Contact con:Trigger.new){
for(Opportunity opp:oppMap.values()){
opp.Email__c=con.Email;
opp.Fax__c = con.Fax;
oppList.add(opp);
}
update oppList;
}
}
If this answers your query,please mark this as solution so that it would be useful for others.
Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 03:14 AM
Hi vinit,
Thanks for your reply as you said you are created a lookup relation to opportunity but i need this trigger to work without creating the relationshipand can you tell me i have another trigger which will fire on updation of opportunity which will cause recurrsion i need to prevent this recurrsion also for the updated opprtunity records.
Thanks in advance..
Re: Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 03:19 AM
Hi Atteli,
I modified the code and now you don't need to create a lookup relationship:-
trigger updateRelatedOpp on Contact (before insert,before update) {
List<Contact> conList = new List<Contact>();
List<Id> idList = new List<Id>();
List<Opportunity> oppList = new List<Opportunity>();
for(Contact newCon : Trigger.new){
Contact oldCon = Trigger.oldMap.get(newCon.Id);
if(oldCon.Email!=newCon.Email || oldCon.Fax!=newCon.Fax){
idList.add(newCon.AccountId);
}
}
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([select id,Email__c,Fax__c from Opportunity where AccountId in:idList]);
for(Contact con:Trigger.new){
for(Opportunity opp:oppMap.values()){
opp.Email__c=con.Email;
opp.Fax__c = con.Fax;
oppList.add(opp);
}
update oppList;
}
}
To prevent the Trigger recursion you can use TriggerHelper class.Sample code is below :-
public class TriggerHelperClass {
private static boolean flagvalue = false;
public static boolean hasAlreadyfired() {
return flagvalue;
}
public static void setAlreadyfired() {
flagvalue = true;
}
}
Use this class in your Apex Trigger as below :-
trigger updateRelatedOpp on Contact (before insert,before update) {
for(Contact newCon : Trigger.new){
if(!TriggerHelperClass.hasAlreadyfired()){
//do your coding here
TriggerHelperClass.setAlreadyFired(); //it would prevent the Trigger to run recursively.
}
If this answers your query,please mark this as solution so that it would be useful for others.
Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 08:16 AM
Hi Vinit,
I'm getting an error
REQUIRED_FIELD_MISSING, Required fields are missing: [Ref ID]: [Ref ID]: Trigger.updateRelatedOpp: line 24, column 1
Re: Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 08:45 AM
HI vinit,
I have a custom field Ref_ID__c which is Required so i added in the code eventhough i'm getting the same error
REQUIRED_FIELD_MISSING, Required fields are missing: [Ref ID]: [Ref ID]: Trigger.updateRelatedOpp: line 24, column 1
trigger updateRelatedOpp on Contact (before insert,before update) {
List<Contact> conList = new List<Contact>();
List<Id> idList = new List<Id>();
List<Opportunity> oppList = new List<Opportunity>();
for(Contact newCon : Trigger.new){
Contact oldCon = Trigger.oldMap.get(newCon.Id);
if(oldCon.Email!=newCon.Email || oldCon.Fax!=newCon.Fax){
idList.add(newCon.AccountId);
}
}
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([select id,Email__c,Fax__c,Ref_ID__c from Opportunity where AccountId in:idList]);
for(Contact con:Trigger.new){
for(Opportunity opp:oppMap.values()){
opp.Ref_ID__c=oppMap.get(opp.ID).Ref_ID__c; /// Added this Line of code
opp.Email__c=con.Email;
opp.Fax__c = con.Fax;
oppList.add(opp);
}
update oppList;
}
}
Re: Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 09:49 AM
Attelli,
Ref ID is required field for opportunity or Contact.I am not sure why you are getting REQUIRED_FIELD_MISSING in update call.This error comes up insert call normally.
Is there any custom validation rule.I would be checking the debug logs to confirm why it is erroring out.
If this answers your query,please mark this as solution so that it would be useful for others.
Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 10:01 AM
HI Vinit,
REF_ID__c is a text tirld and is required on opportunity object, i have two validation rules but those two are not related to this fields...if possible can we connect on gmail..
my mail id is aakumar1202@gmail.com;
IT's urgent for me
Re: Trigger on Contact to update opportunit y
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 10:29 AM
Hi,
I would like to check the debug logs.Can you please check the debug logs and if you can't et it through send me.I sent you a test email.
If this answers your query,please mark this as solution so that it would be useful for others.

