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

Trigger to Update Lookup Field Related to another Lookup Field
Hi,
I have a custom object called Invoice_Header_Object that has a Master-Detail relationship with the Contact (a lookup field on the Contact object called Customer__c)
I also have another lookup field on the custom object that looks up the Account object (lookup field called Account__c)
I have a trigger that updates the Account__c field based on the Contact lookup field (grabs the Account on the Contact) and it works
My question is, how do I update the trigger to have it where if the Contact's Account is changed to another Account, have the trigger update the Account__c to reflect the new Account and also delete the associated Invoice Header from the previous Account
Here is my trigger:
trigger UpdateAccountIDInvoice on Invoice_Header__c (before insert,before update){
if (trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
set<Id> ContactIds = new set<ID>();
for(Invoice_Header__c cm: trigger.new)
{
contactIds.add(cm.customer__c);
}
Map<Id, Id> contactToAccountMap = new Map<Id, Id>();
for(contact c :[select Id, AccountId from contact where id in: contactIds])
{
contactToAccountMap.put(c.Id,c.AccountId);}
for(Invoice_Header__c cm: trigger.new)
{
cm.Account__c = contactToAccountMap.get(cm.customer__c);}
}
}
thank you
I have a custom object called Invoice_Header_Object that has a Master-Detail relationship with the Contact (a lookup field on the Contact object called Customer__c)
I also have another lookup field on the custom object that looks up the Account object (lookup field called Account__c)
I have a trigger that updates the Account__c field based on the Contact lookup field (grabs the Account on the Contact) and it works
My question is, how do I update the trigger to have it where if the Contact's Account is changed to another Account, have the trigger update the Account__c to reflect the new Account and also delete the associated Invoice Header from the previous Account
Here is my trigger:
trigger UpdateAccountIDInvoice on Invoice_Header__c (before insert,before update){
if (trigger.isBefore &&(trigger.isInsert||trigger.isUpdate))
{
set<Id> ContactIds = new set<ID>();
for(Invoice_Header__c cm: trigger.new)
{
contactIds.add(cm.customer__c);
}
Map<Id, Id> contactToAccountMap = new Map<Id, Id>();
for(contact c :[select Id, AccountId from contact where id in: contactIds])
{
contactToAccountMap.put(c.Id,c.AccountId);}
for(Invoice_Header__c cm: trigger.new)
{
cm.Account__c = contactToAccountMap.get(cm.customer__c);}
}
}
thank you

You can write an after update trigger on Contact to do a dummy update on the Invoice Header that fires your trigger, this will re-calculate the lookups.