function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kali charankali charan 

prepopulating the phone field

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.
Best Answer chosen by kali charan
Srinivas SSrinivas S
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.

All Answers

Srinivas SSrinivas S
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.
This was selected as the best answer
kali charankali charan
Thanq srinivas