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
sfdev1sfdev1 

Trigger on Opportunity

Hi
 
I have create a custom Field called PrimaryContact on the Opportunity Object.
 
I want to automatically update the contact for this field by collecting the primarty ContactRole for that opportunity by a Apex Trigger event.
 
 
Thanks
 

Stephen
sfdev1sfdev1
trigger UpdateAccountPrimaryContact on Opportunity (after update) {

    List<Opportunity> OpptoUpdate= new List<Opportunity>();

    OpportunityContactRole[] OppCR = [Select o.ContactId, o.OpportunityId from 
                                      OpportunityContactRole o  where o.IsPrimary = true 
                                      and o.OpportunityId in :Trigger.new];
                                      
                                      
    for (OpportunityContactRole CurOppCR : OppCR){
 OpptoUpdate.Primary_Opportunity_Contact__c = CurOpCR.ContactID
    }


   update OpptoUpdate

}