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

trigger to update a look up field from look up field of another look up field.

Hi Friends,

Scenario: Object A has two contact look up fields.I want  to update the first look up field from  a look up field available on the second look up field.I have accomplished this via a trigger.But I came across something which totally blew away my mind.

I want to populate MasterContactrecord__c field of object A from a look up field Master_Contact_Record__c available on the another lookup on object A -  Opportunity_Contact__c.

Trigger 1: runs successfully and accomplishes the requirement
trigger populate1 on A (before insert,before update) {

Set<Id> set_OpportunityContactIDs = new Set<Id>();

For(A ocr :


Map<Id,Contact> map_MasterContactRecord = new Map<Id,Contact>
    (   [   SELECT  Master_Contact_Record__c
            FROM    Contact
            WHERE   Id IN :set_OpportunityContactIDs
for ( A ocr1: )
        ocr1.MasterContactrecord__c = map_MasterContactRecord.get( ocr1.Opportunity_Contact__c ).Master_Contact_Record__c;
Trigger 2:Saves succesfully but doesn't work.Why?if I used the same relationship  I used here in quey editor,it returns the exact record I want.
trigger populate on A (before insert,before update) {

For(A ocr :
//ocr.MasterContactrecord__c=ocr.Opportunity_Contact__r.Master_Contact_Record__r.Id; This seem correct but doesn't return anything in quey editor.


Is there something I am missing.Pointing to any mistake is highly appreciable!!
I think the second Triggers does not work because in, you only have access to one level of data. So you can access ocr.Opportunity_Contact__c in Trigger 1, but you will not have access to ocr.Opportunity_Contact__r.Master_Contact_Record__c, which goes down two levels. I think you can do some query/map to find out the Master_Contact_Record__c information.
thanks Yuchen.I just came to know that!!