Reply
Regular Contributor
attelli
Posts: 109
0

Trigger on Contact to update opportunity

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.

Super Contributor
Vinit_Kumar
Posts: 645
0

Re: Trigger on Contact to update opportunity

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;
     }
   
     
     

}

Don't forget to give KUDOS if post helped you.

If this answers your query,please mark this as solution so that it would be useful for others.


Regular Contributor
attelli
Posts: 109
0

Trigger on Contact to update opportunity

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..

Super Contributor
Vinit_Kumar
Posts: 645

Re: Trigger on Contact to update opportunity

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.

 

 

}

 

 

Don't forget to give KUDOS if post helped you.

If this answers your query,please mark this as solution so that it would be useful for others.


Regular Contributor
attelli
Posts: 109
0

Trigger on Contact to update opportunity

Hi Vinit,

 

I'm getting an error 

REQUIRED_FIELD_MISSING, Required fields are missing: [Ref ID]: [Ref ID]: Trigger.updateRelatedOpp: line 24, column 1

 

 

 

Regular Contributor
attelli
Posts: 109
0

Re: Trigger on Contact to update opportunity

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;
}

}

 

Super Contributor
Vinit_Kumar
Posts: 645
0

Re: Trigger on Contact to update opportunity

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.

Don't forget to give KUDOS if post helped you.

If this answers your query,please mark this as solution so that it would be useful for others.


Regular Contributor
attelli
Posts: 109
0

Trigger on Contact to update opportunity

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

Super Contributor
Vinit_Kumar
Posts: 645

Re: Trigger on Contact to update opportunity

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.

Don't forget to give KUDOS if post helped you.

If this answers your query,please mark this as solution so that it would be useful for others.