+ Start a Discussion
Nagarjun TNagarjun T 

trigger on contact to update account fax from contact

trigger contaccupdatetrigger on Contact (after update)
{
    List<contact> lstConUpdate = new List<Contact>();
    set<Id> sAccId = new set<Id>();
    for(Contact con: trigger.new){
        saccId.add(con.accountId);
    }
    List<Account> lstaccount = [select fax, (select fax from contacts) from account where id IN: saccId];
    for(Account acc: lstAccount){
        for(Contact con: acc.contacts){
            con.fax = acc.fax;
            lstConUpdate.add(con);
        }
    }
    if(lstConUpdate.size() > 0){
        update lstConUpdate;
    }   
}

Error :--
           Error:Apex trigger contaccupdatetrigger caused an unexpected exception, contact your administrator: contaccupdatetrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0037F000002JwNEQA0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, contaccupdatetrigger: maximum trigger depth exceeded Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate Contact trigger event AfterUpdate: []: Trigger.contaccupdatetrigger: line 16, column 1
Steven NsubugaSteven Nsubuga
I would rewrite your trigger like so.
trigger contaccupdatetrigger on Contact (after update)
{
    List<contact> lstConUpdate = trigger.new;
    set<Id> sAccId = new set<Id>();
    for(Contact con: lstConUpdate ){
        saccId.add(con.accountId);
    }
    Map<Id, Account> accountMap = new Map<Id, Account>([select id, fax from account where id IN: saccId]);
    for(Contact con: lstConUpdate){
        con.fax = accountMap.get(con.AccountId).fax;
    }
    update lstConUpdate;
}

 
Nagarjun TNagarjun T
thq  for ur time @Steven Nsubuge,,, after rewrite this code again it getting error as follows

Error:Apex trigger contaccupdatetrigger caused an unexpected exception, contact your administrator: contaccupdatetrigger: execution of AfterUpdate caused by: System.FinalException: Record is read-only: Trigger.contaccupdatetrigger: line 10, column 1
Steven NsubugaSteven Nsubuga
Make this change
trigger contaccupdatetrigger on Contact (before update)
{
    set<Id> sAccId = new set<Id>();
    for(Contact con: trigger.new){
        saccId.add(con.accountId);
    }
    Map<Id, Account> accountMap = new Map<Id, Account>([select id, fax from account where id IN: saccId]);
    for(Contact con : trigger.new){
        con.fax = accountMap.get(con.AccountId).fax;
    }
}

 
Nagarjun TNagarjun T
@StevenNsubuga
Now there is no error but,,either in contact object or account object fax is not updating,, fax value  remains same
Steven NsubugaSteven Nsubuga
This trigger is meant to update each Contact's fax field with the value of its Account's fax field. The value of the fax field from the Account is used to update the fax field of each of its Contacts.