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
Satish ChandrashekarSatish Chandrashekar 

Update a field in one object based on the common field in the other

HI,

I am new to APEX and hence seeking help to understand if there is a possibility to write a trigger to update a field(Record_ID_1) in Custom_Object_2 after comparing the value of common field (this value is present in both objects) in another Custom_Object_1. These two objects have Master Detail relationship but all the data in these two objects are uploaded by different team members and no possibility of manual entry at both ends

thanks



 
jyothsna reddy 5jyothsna reddy 5
Hi Satish,
For example I want to update a account address in account object automatically updated address in contact.
Please try the below sample code
trigger accaddressUpdateOnContact on Account (After Update) {
    list<account> li = [select id,(select id from contacts) from account where id IN : trigger.new];
    map<id,list<contact>> m = new map<id,list<contact>>();
   
    for(account aa : li){
        
        m.put(aa.id,aa.contacts);
    }
    list<contact> c = new list<contact>();
    for(account aa : trigger.new){
        list<contact> lc = m.get(aa.id);
        for(contact cc: lc){
            cc.mailingcity = aa.BillingCity;
            c.add(cc);
        }
        
    }         
    update c;

Hope it will help you.

Regards,

Jyothsna D
 
Satish ChandrashekarSatish Chandrashekar
Thanks,

Quick question will there be any issues / errors  / modifications ot the above code needed...if the field that i am trying to update through this trigger is the same field which holds the Master Detail relation between those two objects?