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
krish123krish123 

Unable to update parent account fields On Account using in trigger

Hello All,

1. I am trying to update parent account fields on account using trigger.
2. I did't get any error but it's not updated on parent account fields.
Can you please check the below Trigger/Code,..thanks for support..

trigger UpdateItselfAccount on Account (After update) {
    If(TriggerHelper.isAccountTriggersAreRunning == false){
 //   boolean isAccountTriggersAreRunningt = true;    
    List<Account> al=trigger.new;
    Set<ID> accPIDs =  new set<ID>();
    for(Account acclist:al){
        if(acclist.ParentId != Null)
          accPIDs.add(acclist.ParentId);
    }
    list<Account> Accchiledlist=[SELECT id,Name,Rating,ParentId FROM Account where ParentId= : accPIDs];
    system.debug('Account parent list@@@@@'+Accchiledlist);
    for(Account a:trigger.new){
      for(Account clist:Accchiledlist){
           clist.Rating =a.Rating;
         system.debug('Parent rating@@@@@'+clist.Rating);
      }
    }
    if(Accchiledlist.size()>0 ){
        If(TriggerHelper.isAccountTriggersAreRunning =! false){
        //system.debug('isAccountTriggersAreRunning@@@@@'+TriggerHelper.isAccountTriggersAreRunning);  
             update Accchiledlist;
                }
        }
  }
}



 
Best Answer chosen by krish123
Arunkumar RArunkumar R
Hi Krish,

You trigger code seems quite complex, i just simplified your logic except to avoid recursive. You can just add you recursive boolean flag and try it..
 
trigger UpdateItselfAccount on Account (After update)
{
    List<Account> parentAccounts = new List<Account>();
    
    for(Account currAcc : Trigger.New)
    {
        if(currAcc.parentId != null)
        {
            Account acc = new Account();
            acc.Id = currAcc.parentId;
            acc.Rating = currAcc.Rating;
            parentAccounts.add(acc);
        }
    }
    
    if(parentAccounts.size() > 0)
    update parentAccounts;
}

 

All Answers

Mudasir WaniMudasir Wani

Hello Krish,

If you want to perform the operation on same Object then you better go with workflow.

To update same object you need to update it assynchronously which can be done as follows. 
You can update the account by placing the code in a future method.
Remember you can pass primitive data types as parameters.

Create a class
create a method with future annotation 
Place the code there 

public Class youClassName{

@Future
public static void updateAccount(Parameters--Primitive data type only --say a set of account Ids)
      //fetch the accounts and update the field you want and then save.
}

Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
Arunkumar RArunkumar R
Hi Krish,

You trigger code seems quite complex, i just simplified your logic except to avoid recursive. You can just add you recursive boolean flag and try it..
 
trigger UpdateItselfAccount on Account (After update)
{
    List<Account> parentAccounts = new List<Account>();
    
    for(Account currAcc : Trigger.New)
    {
        if(currAcc.parentId != null)
        {
            Account acc = new Account();
            acc.Id = currAcc.parentId;
            acc.Rating = currAcc.Rating;
            parentAccounts.add(acc);
        }
    }
    
    if(parentAccounts.size() > 0)
    update parentAccounts;
}

 
This was selected as the best answer
krish123krish123
Hello Arun,

Thanks for your help,it's works good. Thanks....