Skip to main content
kali charan 님이 #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개
  1. 2016년 5월 25일 오전 10:22
    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.