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
sufyan p ksufyan p k 

I want to write a trigger to update field in opportunity when there is a change in contact relations: lookup from account to contact. Lookup from account to opportunity. Nb: no relation from contact to opportunity

I want to write a trigger to update field in opportunity(email field) when there is a change in contact  e mail field
relations: lookup from account to contact.  Lookup from account to opportunity.  
  Nb: no relation from contact to opportunity
Best Answer chosen by sufyan p k
ayu sharma devayu sharma dev
Hello you can try this below trigger,
 
trigger ContactTrigger on Contact (after update) {

    Map<Id, String> mapAccIdToEmail = new Map<Id, String>();
    List<Opportunity> oppListToUpdate = new List<Opportunity>();

    for( Contact ct : Trigger.new ){
        if( ct.Email != Trigger.oldMap.get( ct.Id ).Email ){
            mapAccIdToEmail.put( ct.AccountId, ct.Email );
        }
    }

    for( Opportunity opp : [ SELECT Id, Email FROM Opportunity WHERE AccountId IN: mapAccIdToEmail.keyset() ] ){
        opp.Email = mapAccIdToEmail.get( opp.AccountId );
        oppListToUpdate.add( opp );
    }

    if( oppListToUpdate.size() > 0 ){
        update oppListToUpdate;
    }

}

Let me know if any problem arises.

Thanks

All Answers

ayu sharma devayu sharma dev
Hello,

Just want to confirm one thing, are you utilizing the standard lookup relationship or if you have created some custom relations between Account, Contact and Opportunity.
As your question I am getting the relations as:
Contact's Parent - Account,
Account's Parent - Opportunity.

Please make me correct then I can develop the code.

Thanks,
Ayush
sufyan p ksufyan p k
hey ayu sharma 
    iam  not utilising the standard lookup relationship, there is no relation ship between contact and opportunity
there is lookup relationship from contact to account and there is lookup from opportunity to account ,account act as parent for contact and opportunity     
 thanks 
  sufyan
ayu sharma devayu sharma dev
Hello you can try this below trigger,
 
trigger ContactTrigger on Contact (after update) {

    Map<Id, String> mapAccIdToEmail = new Map<Id, String>();
    List<Opportunity> oppListToUpdate = new List<Opportunity>();

    for( Contact ct : Trigger.new ){
        if( ct.Email != Trigger.oldMap.get( ct.Id ).Email ){
            mapAccIdToEmail.put( ct.AccountId, ct.Email );
        }
    }

    for( Opportunity opp : [ SELECT Id, Email FROM Opportunity WHERE AccountId IN: mapAccIdToEmail.keyset() ] ){
        opp.Email = mapAccIdToEmail.get( opp.AccountId );
        oppListToUpdate.add( opp );
    }

    if( oppListToUpdate.size() > 0 ){
        update oppListToUpdate;
    }

}

Let me know if any problem arises.

Thanks
This was selected as the best answer
sufyan p ksufyan p k
thanks ayush for your support