You need to sign in to do that
Don't have an account?

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 : trigger.new)
if(ocr.Opportunity_Contact__c!=null)
{
set_OpportunityContactIDs.add(ocr.Opportunity_Contact__c);
}
Map<Id,Contact> map_MasterContactRecord = new Map<Id,Contact>
( [ SELECT Master_Contact_Record__c
FROM Contact
WHERE Id IN :set_OpportunityContactIDs
]
);
for ( A ocr1: trigger.new )
{
if(ocr1.Opportunity_Contact__c!=null)
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 : trigger.new)
{
if(ocr.Opportunity_Contact__r.Master_Contact_Record__c!=null)
{
ocr.MasterContactrecord__c=ocr.Opportunity_Contact__r.Master_Contact_Record__c;
//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!!
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 : trigger.new)
if(ocr.Opportunity_Contact__c!=null)
{
set_OpportunityContactIDs.add(ocr.Opportunity_Contact__c);
}
Map<Id,Contact> map_MasterContactRecord = new Map<Id,Contact>
( [ SELECT Master_Contact_Record__c
FROM Contact
WHERE Id IN :set_OpportunityContactIDs
]
);
for ( A ocr1: trigger.new )
{
if(ocr1.Opportunity_Contact__c!=null)
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 : trigger.new)
{
if(ocr.Opportunity_Contact__r.Master_Contact_Record__c!=null)
{
ocr.MasterContactrecord__c=ocr.Opportunity_Contact__r.Master_Contact_Record__c;
//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 Trigger.new, 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!!