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
MMuneeeMMuneee 

Scenrio: Trigger to update account phone number with contact phone numbaer

here am getting error like"Error: Invalid Data.  Review all error messages below to correct your data. Apex trigger phupdate caused an unexpected exception, contact your administrator: phupdate: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.phupdate

trigger phupdate on Contact (after insert,after update) {
List<account> li=new list<Account>();
for(Contact c: trigger.new)
{
Account a=[select Phone from account where id=:c.Accountid ];
a.Phone= c.MobilePhone;
li.add(a);
}
update li;
}
SarfarajSarfaraj
Use this,
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)
		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.MobilePhone;
			li.add(a);
		}
	}
	update li;
}


shiv@SFDCshiv@SFDC
Hi Muneendar ,

An account can have multiple contacts. Then which contact's phone you want to update on Account phone number. Please describe your requirment in detail so we can give you right solution.

Thanks,
Shiv