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

trigger to update lookup fields in 2 object lookup fields

trigger to update lookup fields in 2 object lookup fields My requirement is :
ex:- accounts and contacts objects has common field city if i update city field in accounts then it must update in contacts also.

 i am having 2 lookup fields in 2 different objects which is a common field named  "city"

If i update a city name using lookup for example california is changed to *washington* the change must update all records which have california must be changed to washington the changes must reflect in 2 objects same time.

Damien Phillippi033905702927186443Damien Phillippi033905702927186443
I have two different solutions for two different problems since I'm not sure what you are trying to say.

I'm having a hard time following what you are saying, but it sounds like you have an Account and a Contact that look up to a City object.  When you change the city name you want it to reflect in both objects?  You actually shouldn't need any trigger for this case if they are looking up to the same object.  It would be reflected automatically.

If instead you mean that City is a field and not a lookup on Account/Contact, but they have a lookup to each other then you approach it a little differently.  A trigger could query for the associated other record and you can copy over the data from what you queried.  You could alternatively have one of those fields be a formula field and have it simply be read only and grab the value contained in the other's field.
Ankit Gupta@ DeveloperAnkit Gupta@ Developer

You need to create two trigger on both objects with a help class to control the recursion condition. One on parent object and one for child object.