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
Chinna SfdcChinna Sfdc 

Updating trigger Help

Hi Team,

We have an requirement as mentioend below
1.When Phone # field is updated in Account , the same number has to get updated in Contact -> Phone # field and if the Phone # field in Contact is updated and the same Phone # has to be populated in Account -> Phone # field.

Through Trigger how we can acheive this,Can any one please Help me on this.

Thanks
Sukesh Kumar 33Sukesh Kumar 33
What if the Account has multiple Contacts, which contacts Phone # to be considered ? 
Chinna SfdcChinna Sfdc
Hi Sukesh,

Thanks for your reply.

If I update account Phone #  field then related contacts Phone field should update. this is what am expecting.I undrstand your question but since i don't have complete requirement, can you please update me one Account and one contact record.Based on your code i will modify accordingly.

Thanks
Sukesh Kumar 33Sukesh Kumar 33
Use a trigger on Account in the following way to update all the related Contact
trigger accountTrigger on Account (after Update) { 

	//Use the Appropriate Phone field here for both Contact and Account
	for(Contact objContact : [SELECT Id,
									 Phone,
									 Account.Phone
								FROM Contact
							   WHERE AccountId IN : Trigger.New()]) {

		lstContactToUpdate.add(new Contact(Id = objContact.Id,
										   Phone = objContact.Account.Phone));
	}

	update lstContactToUpdate;
}

 
Chinna SfdcChinna Sfdc
Hi Sukesh,

Getting Error like "Variable does not exist: lstContactToUpdate at line 7 column 12".Please help me on this.

Thanks
Sukesh Kumar 33Sukesh Kumar 33
List<Contact> lstContactToUpdate = new List<Contact>()
Add this one above the for loop
 
Chinna SfdcChinna Sfdc
Hi Sukesh,

Am sorry ...We are trying to get below trigger.But still  getting error like "Variable does not exist: Trigger at line 4 column 97"

trigger AccountTrigger on Account (after Update){ 
    List<Contact> lstContactToUpdate = new List<Contact>();
   
    for(Contact objContact : [SELECT Id,Phone, Account.Phone FROM Contact  WHERE AccountId IN : Trigger.New()]){
        lstContactToUpdate.add(new Contact(Id = objContact.Id,Phone = objContact.Account.Phone));
    }
    update lstContactToUpdate;
}

Thanks
Sukesh Kumar 33Sukesh Kumar 33
Try this way
trigger accountTrigger on Account (after Update) { 
        
       List<Contact > lstContactToUpdate = new List<Contact >();
	//Use the Appropriate Phone field here for both Contact and Account
	for(Contact objContact : [SELECT Id,
									 Phone,
									 Account.Phone
								FROM Contact
							   WHERE AccountId IN : Trigger.New]) {

		lstContactToUpdate.add(new Contact(Id = objContact.Id,
										   Phone = objContact.Account.Phone));
	}

	update lstContactToUpdate;
}

 
Chinna SfdcChinna Sfdc
Hi Sukesh,

Great..It is working fine But one more help hear  Account to contact phone number is updating fine.  similorly Contact to Account also needs to be update.Please tell me how to do that. It will great help for us.

Thanks
Sukesh Kumar 33Sukesh Kumar 33

This is where the question of Multiple Contacts come.
If we start updating Account Phone # on Update of any Contact this would change the data. So it would be better if we could get the requirement clear.

Please mark the above answer as best if it was helpfull to you!!