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

Best Answer chosen by Admin (Salesforce Developers) 
jvelazquezjvelazquez

Hi, I  Could solve this using @future.

Thanks sfdcfox for sharing me the piece of code.

 

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

    if(!system.isfuture()) {
    getleadsource.forrecords(Trigger.newmap.keyset());
  }
                        
         
}

 

 

public class getleadsource {

  @future 
  public static void forrecords(set<id> oppids) {
    map<id, opportunity> opps = new map<id, opportunity>();
    for(opportunitycontactrole record:[select contact.leadsource,contact.Lead_Source_Most_Recent_Eloqua__c,opportunityid from opportunitycontactrole where opportunityid in :oppids]) {
      opps.put(record.opportunityid, new opportunity(id=record.opportunityid, leadsource=record.contact.leadsource, Lead_Source_Most_Recent__c=record.contact.Lead_Source_Most_Recent_Eloqua__c));
    }
  
    update opps.values();
  }
}

 

 

All Answers

CloudHugger2CloudHugger2
Do you have to use after insert, after update ? Could you use the before insert/update events instead?
NHKNHK

If your requirement is to capture the value of LeadSource from contact to related opportunity, then you can write trigger on Contact and update the corresponding opportunity records.

jvelazquezjvelazquez

Hi, I  Could solve this using @future.

Thanks sfdcfox for sharing me the piece of code.

 

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

    if(!system.isfuture()) {
    getleadsource.forrecords(Trigger.newmap.keyset());
  }
                        
         
}

 

 

public class getleadsource {

  @future 
  public static void forrecords(set<id> oppids) {
    map<id, opportunity> opps = new map<id, opportunity>();
    for(opportunitycontactrole record:[select contact.leadsource,contact.Lead_Source_Most_Recent_Eloqua__c,opportunityid from opportunitycontactrole where opportunityid in :oppids]) {
      opps.put(record.opportunityid, new opportunity(id=record.opportunityid, leadsource=record.contact.leadsource, Lead_Source_Most_Recent__c=record.contact.Lead_Source_Most_Recent_Eloqua__c));
    }
  
    update opps.values();
  }
}

 

 

This was selected as the best answer