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
ChinnoduChinnodu 

HI , i am phasing this issue variable doesnot exist on trigger

HI,
i am phasing this issue variable  doesnot exist.

trigger updatephonecontact on Account (after update)
{
 for(account acc : trigger.new)
 {
 list<contact> listcon= [select phone from contact where accountid=:acc.id];
 }
/for(Contact ct : listcon)
 {
 
 ct.phone=acc.phone ;// i got error at his line can ypo pls help me
 }
 update listcon;
}
hpmohanhpmohan
'acc' is out of scope. Second for loop has no idea what 'acc' is.
ChinnoduChinnodu
HI Mohan,

This is my requirement,

on Account object .it contain records and child related list contacts. Where account phone number and contact phone number are same. let suppose any point of time I would like to update to the phone number of Account at the same time I would like to update it’s associated contact phone also  .


Regadrds,
viswa
sunny522sunny522
Hi Viswa,
        Please go through the sample code in below link
        http://salesforceglobe4u.blogspot.in/2015/12/update-all-contacts-phone-number-with.html
Let me know in case of any issues.
hpmohanhpmohan
for(account acc : trigger.new) // scope of 'acc' begins here
 {
 list<contact> listcon= [select phone from contact where accountid=:acc.id];
 }
for(Contact ct : listcon) scope of 'acc' ends here, and a new object 'ct' starts here
 {
 
 ct.phone=acc.phone ;// this for loop will definitely ask, where my 'acc' object is.
 }
Please try solve your issue like this.
 
//PSEUDOCODE

triggerOnAccount(after update)
{
//get all 'id' s of incoming objects

	set<id> accList = new set<id>();
	for ( account acc : trigger.new)
	{
		accList.add(acc.id);
	}

// query all contacts, those belong to the account in 'accList'

	list<Contact> conList = [SELECT id,phone FROM contact WHERE Account.id =:accList];

// do the update phone number part here

	for (Contact c :conList )
	{
		c.phone = c.account.phone;
	}
//update contacts

	update conList
}
please see some best practices here.https://developer.salesforce.com/page/Apex_Code_Best_Practices

Thanks
hpm