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
bharath kumar 52bharath kumar 52 

Update a parent object's comment field when a child object's comment field is updated

Hi All,

Can someone give me the logic to write a trigger to update a parent object's comment field when a child object's comment field is updated.
 

deepak balur 19deepak balur 19
List<ParentObject> POUpdList = new List<ParentObject>();
for (Child_Obj CJ: trigger.new){
   ParentObject POPrep = new ParentObject();
    POPrep.Id = CJ.ParentId;
    POPrep .Comment=CJ.Comment;
    POUpdList.add(POPrep);
    
}
    update POUpdList;

 
bharath kumar 52bharath kumar 52

Hi Deepak,

That wouldn't work. Because you are trying to create an account from a contact......

I have mentioned "on update of child update the parent". But, the solution given is the other way round.

Thanks for your time.

deepak balur 19deepak balur 19
Neither have I mentioned nor Contact anywhere. I have provided the DML operation of update not create.
I have guided you to loop thru the Child and update the Parent. It can be applied generically to any Child-Parent combination you want.
nitin sharma 190nitin sharma 190
You can try below given code ,Updation of  comment field on the child object field in turn will upate commnent field oin the account parent object 

trigger Crunch on Contact (Before update) {

map<id,account>l1=new map<id,account>(l);
list<account>acc=new list<Account>();
Account up;

Map<id,account>m=new Map<id,account>();
for(contact c:trigger.new)
{

m.put(c.accountid,null);

}
list<account> l=[select id, comment__c from account where id in:m.keyset()];

for(contact d:trigger.new)
{

 up=(l1.get(d.accountid));
up.comment__c=d.comment__c;
}
update up;
}