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
Prince PranavPrince Pranav 

Contact Trigger against opportunity

Hi all,
Need help in trigger,

Whenever a Contact is updated and only if any of the values for the below fields changed(Oldmap and NewMap values.)

Member Id
Preferred language
Phone

For all the contacts verified against Opportunity Contact roles with IsPrimary = true. Then fetch all the Opportunities and update the values for corresponding 3 fields in Opportunity record.

Costco Member Id
Preferred language
Phone
Best Answer chosen by Prince Pranav
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Prince

FInd the code for reference : 
TRIGGER_ON_CONTACT after update and after insert{
Set < Id > setOfContactIds = new Set < Id > ();
for(Contact contactRecord : triggerNew ) {
if(contactRecord.MEMBER_ID != null && contactRecord.P_LANG != null && contactRecord.PHONE != null && (
triggerOldMap == null || (
contactRecord.MEMBER_ID != triggerOldMap.get(contactRecord.Id).MEMBER_ID || contactRecord.P_LANG != triggerOldMap.get(contactRecord.Id).P_LANG  || contactRecord.PHONE != triggerOldMap.get(contactRecord.Id).PHONE ))) 
setOfContactIds .add(contactRecord.Id);
}
if(setOfContactIds .isEmpty())
return;

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

for(OpportunityContactRole oppContactRole : [SELECT Id, OpportunityId, ContactId,Contact.MEMBER_ID ,Contact.P_LANG  , Contact.PHONE IsPrimary FROM OpportunityContactRole WHERE ContactId IN : setOfContactIds  AND IsPrimary  = True]) {
Opportunity opp = new Opportunity (Id = oppContactRole .OpportunityId);
opp .COSTCO_MEMBER_ID = oppContactRole.Contact.MEMBER_ID;
opp.P_LANG = oppContactRole.Contact.P_LANG .;
opp.PHONE =oppContactRole.Contact.PHONE.;
lstOfOpportunity .add(opp);
}

If(!lstOfOpportunity.isEmpty())
UPDATE lstOfOpportunity;

Cheers!!!!