function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jvelazquezjvelazquez 

how to write Trigger to update Opportunity field from contact field

Hi,

 

In my organisation an Opportunity is created from Contact related list. I have two feilds

LeadSource on Contact

LeadSource on Opportunity

 

 I have a requirement where I am supposed to update a feild value Lead Source in opportunity from Contact field Lead Source

 

I have written the below trigger which is not working since I am using after insert, after update in my trigger call.

 

trigger OpportunityLeadSourceUpdate on Opportunity (after insert, after update) {

    List<Contact> conList = new List<Contact>();
          
                
              
                    
                    OpportunityContactRole ContactRoles = [select OpportunityID, ContactID from OpportunityContactRole where OpportunityID =: trigger.new];
                    
                       System.debug('@@@@@@@@@@@@@@@@@@@@@'+ContactRoles );
                    
                        Contact c= [Select id, LeadSource from Contact where id =: ContactRoles.ContactID ];
                        Opportunity opp = [Select LeadSource from Opportunity where id =: ContactRoles.OpportunityID];
                                                                            
                           opp.LeadSource = c.LeadSource;
                                                                                                      
                     System.debug('@@@@@@@@@@@@@@@@@@@@@'+ opp.LeadSource);
                     
           
                        
         
}

 

I am not able to capture the value into opp.LeadSource since the trigger is firing after insertion.

 

Is there any workaround for this

 

Please help me

 

Thanks

Puja_mfsiPuja_mfsi

Hello,

I have some query  and suggestion regarding your requirement:

 

1. There any more than one OpportunityContactRole related to one opportunity, then there are more than one contact record so how you identify ,which contact lead source value put into the corresponding opportunity ?

2. If you need to update any fields of the same object in which you write trigger ,u should use before event 

i.e you need to write the below trigger on before event.

 

 

jvelazquezjvelazquez

Hi,

 

I want to copy the lead source from the primary contact.