+ Start a Discussion
Hari Krishnan 43Hari Krishnan 43 

Update a custom contact field from recently updated opportunity

Hi Team,

The opportunity have a lookup to contact and these contacts are linked to more than one opportunities.
My requirement here is to find the rejected/Lost opportunities for that contact and update the rejected reason values from most recently updated opportunity to that contact.

The field rejected reason in opportunity is a picklist field and the values need to be updated to a text or a picklist custom contact field.

Thanks,
Hari
AbhishekAbhishek (Salesforce Developers) 
Hi Hari,

Sample Code:-

trigger OpportunityTrigger on Opportunity(before insert , before update){
    if(trigger.isBefore && trigger.isInsert){
        for(Opportunity OppObj : Trigger.New ){
            if(OppObj.stageName == 'Option1'){ //Opportunitu stage name is option 1
                OppObj.Contact_Status__c = 'abc';
            }
        }
    }
    if(trigger.isBefore && trigger.isUpdate){
        for(Opportunity OppObj : Trigger.New ){
            if(trigger.oldMap.get(OppObj.id).IsClosed != OppObj.IsClosed && OppObj.IsClosed == true ){ // Opportunity is Closed
                OppObj.Contact_Status__c = 'abc';
            }
        }
    }
}


For your Reference, you can check the below blogs,

https://developer.salesforce.com/forums/?id=906F00000008ytiIAA

https://trailblazers.salesforce.com/answers?id=9063A000000peWTQAY


I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.