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
rammramm 

how can i auto upadte account phone field to contact phone field? using triggers

Best Answer chosen by ramm
sfdcMonkey.comsfdcMonkey.com
hi Ashoka
use below contact trigger
trigger phupdate on Contact (after insert,after update) {
    List<account> li=new list<Account>();
    List<Id> ids = new List<Id>();
    for(Contact c: trigger.new){
       if(c.AccountId != null){
        ids.add(c.AccountId);
         }
        }
    Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Phone From Account Where Id In :ids]);
    for(Contact c: trigger.new)
    {
        Account a = accountMap.get(c.AccountId);
        if(a != null)
        {
            a.Phone= c.phone;
            li.add(a);
        }
    }
    update li;
}
let me inform if it helps you
Thanks
 

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Ashoka,

Here is your trigger code:
 
trigger updateAccountPhone on Contact(before insert,before update){
Set<id> accIdSet = new Set<Id>();

	for(Contact con:trigger.new){
		if(con.accountId<>null){
			accIdSet.add(con.accountId);
		}
	}
	
	List<Account> accList = [Select id,phone from Account where id in:accIdSet];
	
	if(accList<>null && accList.size()>0){
		for(Contact con:trigger.new){
			for(Account acc:accList){
				if(con.accountId=acc.id){
					con.phone = acc.phone;
					break;
				}
			}
		}
	}
}

Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv
rammramm
 thank u apoorv  i am geeting error  Variable does not exist: accountId at line 6  really i struck up accountid
Apoorv Saxena 4Apoorv Saxena 4
Hi Ashoka,

Please make sure you are using the exact same code provided below:
 
trigger updateAccountPhone on Contact(before insert,before update){
Set<id> accIdSet = new Set<Id>();

	for(Contact con:trigger.new){
		if(con.accountId<>null){
			accIdSet.add(con.accountId);
		}
	}
	
	List<Account> accList = [Select id,phone from Account where id in:accIdSet];
	
	if(accList<>null && accList.size()>0){
		for(Contact con:trigger.new){
			for(Account acc:accList){
				if(con.accountId==acc.id){
					con.phone = acc.phone;
					break;
				}
			}
		}
	}
}

Please let me know how this works for you, mark this as Solved if this helps you so that others can view it as a proper solution.

Thanks,
​Apoorv
sfdcMonkey.comsfdcMonkey.com
hi Ashoka
use below contact trigger
trigger phupdate on Contact (after insert,after update) {
    List<account> li=new list<Account>();
    List<Id> ids = new List<Id>();
    for(Contact c: trigger.new){
       if(c.AccountId != null){
        ids.add(c.AccountId);
         }
        }
    Map<Id, Account> accountMap = new Map<Id, Account>([Select Id, Phone From Account Where Id In :ids]);
    for(Contact c: trigger.new)
    {
        Account a = accountMap.get(c.AccountId);
        if(a != null)
        {
            a.Phone= c.phone;
            li.add(a);
        }
    }
    update li;
}
let me inform if it helps you
Thanks
 
This was selected as the best answer
rammramm
thank u actually i struck with vaidations now clear , at lost i got thank u apoorv