Skip to main content
kali charan preguntó en #Apex
HI,

I have field called phone in contact when i inserted the new contact or updated the contact of related account the account phone should be prepoulated in the contact phone field of the existing account using trigger.

Thanks in advance.
2 respuestas
  1. 25 may 2016, 10:22 a.m.
    Hi Charan,

    Please find the following solution which is tested -

    trigger ContactTrigger on Contact (before insert, before update) {

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

    for(Contact con : trigger.new) {

    accIds.add(con.AccountId);

    }

    Map<Id,Account> accountMap = new Map<Id,Account>(

    [select Phone from Account where id in: accIds]

    );

    for(Contact con : trigger.new) {

    con.phone = accountMap.get(con.accountId).Phone;

    }

    }

    ------------

    Thanks,

    Srinivas

    - Please mark as solution if your problem is resolved.