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
ch ranjithch ranjith 

Not updating the field contact when i insert the field in account

I have contact_update_fiels__c field in account when update or insert record in account the field value should be copied to all related contacts field update_from_account__c its not updating with below code 

trigger rt on Account (after insert,after update)
{
set<id> set1=new set<id>();
  list<contact> conlistnew=new list<contact>();
  for(account acc:trigger.new)
  {
    set1.add(acc.id);
  } 
  list<contact> conlist=[select id,name,accountid from contact where accountid in:set1];
 
  for(account tempacc:trigger.new)
  {
  for(contact tempcon:conlist)
  {
   if(tempacc.id==tempcon.id)
   {
   tempacc.contact_update_fiels__c=tempcon.update_from_account__c;
   conlistnew.add(tempcon);
   }
  }
  }
  update conlistnew;
}
Best Answer chosen by ch ranjith
Sonam_SFDCSonam_SFDC
if(tempacc.id==tempcon.id) - you are comparing the contact ID to the account ID which will always be different hence the loop will never execute.

Try it this way:

if(tempacc.id==tempcon.accountid)

All Answers

Sonam_SFDCSonam_SFDC
if(tempacc.id==tempcon.id) - you are comparing the contact ID to the account ID which will always be different hence the loop will never execute.

Try it this way:

if(tempacc.id==tempcon.accountid)
This was selected as the best answer
ShashForceShashForce
This line will never be true:

if(tempacc.id==tempcon.id)

Hence, your trigger is not working. Please change it as below:

if(tempacc.id==tempcon.accountid)

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank