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
KR_ForceKR_Force 

Account heirarchy traverse...called a method in before update trigger, which is not updating the fields.

Here is my method, code works fine till line 65 and its getting executed the conditions 6 to 10(debug staments) but the fields are not getting updated , any clue?
  1.   public static void AccountUltimateRevenue(Map<ID,Account> newActMap,Map<ID,Account> oldActMap,List<Account>newActList){
  2.            List<Account>actList=new List<Account>();
  3.            List<Account>aList;
  4.            Set<ID>parentActList=new Set<ID>();
  5.            List<Account>uParents=new List<Account>();
  6.            Map<ID, List<Account>> uActListMap = new Map<ID, List<Account>>();
  7.             for(Account a:newActList) 
  8.             {
  9.             Account a1=oldActMap.get(a.id);
  10.            // To check whether Parent Account was updated and not null
  11.            if((newActMap.get(a.Id).parentID!=oldActMap.get(a.Id).parentID && newActMap.get(a.Id).parentID<>null))
  12.             {
  13.             ActList.add(a);
  14.             parentActList.add(newActMap.get(a.Id).parentID);
  15.             System.debug('old+++++++++'+a1.parentID+'new++++++++++++++++'+a.parentID);
  16.             System.debug('ActList+++++++++'+ActList);
  17.             }
  18.             else 
  19.             if(newActMap.get(a.Id).parentID==null)
  20.             {   System.debug('No Parent**:');
  21.              uParents.add(a);
  22.                
  23.             }
  24.      }       
  25.    // Checking if Parent Account list not null pulling all corresponding Ultimate parent revenue and Ultimate parent vertical values from parent accounts.
  26.         if(parentActList!= null && parentActList.size()>0){
  27.             //system.debug( 'Parent Account list:'+parentActList);               
  28.             map<Id, Account> uActMap=new map<Id, Account>([select Id,AnnualRevenue,OS_Industry_Sector__c, Ultimate_Parent_Vertical__c, Ultimate_Parent_Revenue__c,
  29.                                                            ParentId, Parent.OS_Industry_Sector__c, Parent.AnnualRevenue,
  30.                                                            Parent.ParentId, Parent.Parent.OS_Industry_Sector__c, Parent.Parent.AnnualRevenue,
  31.                                                            Parent.Parent.ParentId, Parent.Parent.Parent.OS_Industry_Sector__c, Parent.Parent.Parent.AnnualRevenue,
  32.                                                            Parent.Parent.Parent.ParentId, Parent.Parent.Parent.Parent.OS_Industry_Sector__c, Parent.Parent.Parent.Parent.AnnualRevenue
  33.                                                            from Account where ID IN :parentActList]);     
  34.                for(Account a: newActList)
  35.                {
  36.                 Account ParentAccount = uActMap.get(a.ParentId);
  37.                 System.debug('Parent ID**:'+ a.ParentId);
  38.                 if (ParentAccount.ParentId ==null){
  39.                 System.debug('Condition 1**:');
  40.                     a.Ultimate_Parent_Vertical__c = ParentAccount.OS_Industry_Sector__c;
  41.                     a.Ultimate_Parent_Revenue__c = ParentAccount.AnnualRevenue;
  42.                     }
  43.                 else if (ParentAccount.Parent.ParentId ==null){
  44.                 System.debug('Condition 2**:');
  45.                          a.Ultimate_Parent_Vertical__c = ParentAccount.Parent.OS_Industry_Sector__c;
  46.                          a.Ultimate_Parent_Revenue__c = ParentAccount.Parent.AnnualRevenue;
  47.                          }
  48.                 else if (ParentAccount.Parent.Parent.ParentId ==null){
  49.                 System.debug('Condition 3**:');
  50.                          a.Ultimate_Parent_Vertical__c = ParentAccount.Parent.Parent.OS_Industry_Sector__c;
  51.                          a.Ultimate_Parent_Revenue__c = ParentAccount.Parent.Parent.AnnualRevenue;
  52.                          }
  53.                 else if (ParentAccount.Parent.Parent.Parent.ParentId ==null){
  54.                 System.debug('Condition 4**:');
  55.                          a.Ultimate_Parent_Vertical__c = ParentAccount.Parent.Parent.Parent.OS_Industry_Sector__c;
  56.                          a.Ultimate_Parent_Revenue__c = ParentAccount.Parent.Parent.Parent.AnnualRevenue;
  57.                          }
  58.                 else if (ParentAccount.Parent.Parent.Parent.Parent.ParentId ==null){
  59.                 System.debug('Condition 5**:');
  60.                          a.Ultimate_Parent_Vertical__c = ParentAccount.Parent.Parent.Parent.Parent.OS_Industry_Sector__c;
  61.                          a.Ultimate_Parent_Revenue__c = ParentAccount.Parent.Parent.Parent.Parent.AnnualRevenue;
  62.                          }
  63.                }
  64.         }
  65.                              
  66.                 if(uParents.size()>0)
  67.                 {  
  68.                  List<Account> uChild=new List<Account>([select Id,AnnualRevenue,OS_Industry_Sector__c, Ultimate_Parent_Vertical__c, Ultimate_Parent_Revenue__c,
  69.                                                            ParentId, Parent.OS_Industry_Sector__c, Parent.AnnualRevenue,
  70.                                                            Parent.ParentId, Parent.Parent.OS_Industry_Sector__c, Parent.Parent.AnnualRevenue,
  71.                                                            Parent.Parent.ParentId, Parent.Parent.Parent.OS_Industry_Sector__c, Parent.Parent.Parent.AnnualRevenue,
  72.                                                            Parent.Parent.Parent.ParentId, Parent.Parent.Parent.Parent.OS_Industry_Sector__c, Parent.Parent.Parent.Parent.AnnualRevenue from Account where ParentId IN :uParents OR Parent.ParentId IN :uParents OR Parent.Parent.ParentId IN :uParents OR Parent.Parent.Parent.ParentId IN :uParents]);
  73.            for(Account a: uChild)
  74.                {
  75.                if (a.ParentId ==null){
  76.                 System.debug('Condition 6**:');
  77.                     a.Ultimate_Parent_Vertical__c = a.OS_Industry_Sector__c;
  78.                     a.Ultimate_Parent_Revenue__c = a.AnnualRevenue;
  79.                     }
  80.                 else if (a.Parent.ParentId ==null){
  81.                 System.debug('Condition 7**:'+a.Parent.OS_Industry_Sector__c+a.Parent.AnnualRevenue);
  82.                   //a.Ultimate_Parent_Vertical__c = a.Parent.OS_Industry_Sector__c;
  83.                          a.Ultimate_Parent_Vertical__c = a.Parent.OS_Industry_Sector__c;
  84.                          a.Ultimate_Parent_Revenue__c = a.Parent.AnnualRevenue;
  85.                          }
  86.                 else if (a.Parent.Parent.ParentId ==null){
  87.                 System.debug('Condition 8**:');
  88.                          a.Ultimate_Parent_Vertical__c = a.Parent.Parent.OS_Industry_Sector__c;
  89.                          a.Ultimate_Parent_Revenue__c = a.Parent.Parent.AnnualRevenue;
  90.                          }
  91.                 else if (a.Parent.Parent.Parent.ParentId ==null){
  92.                 System.debug('Condition 9**:');
  93.                          a.Ultimate_Parent_Vertical__c = a.Parent.Parent.Parent.OS_Industry_Sector__c;
  94.                          a.Ultimate_Parent_Revenue__c = a.Parent.Parent.Parent.AnnualRevenue;
  95.                          }
  96.                 else if (a.Parent.Parent.Parent.Parent.ParentId ==null){
  97.                 System.debug('Condition 10**:');
  98.                          a.Ultimate_Parent_Vertical__c = a.Parent.Parent.Parent.Parent.OS_Industry_Sector__c;
  99.                          a.Ultimate_Parent_Revenue__c = a.Parent.Parent.Parent.Parent.AnnualRevenue;
  100.                          }
  101.                }
  102.                }
  103.                
  104.            
  105.         }
Best Answer chosen by KR_Force
Abdel-Ali BOULIKAAbdel-Ali BOULIKA
Hello,
I think your code will work fine if child account are updated => the fields will be updated using field values from there parents accounts.
This first scenario will work because I guess the "List<Account>newActList" is coming from Trigger.New, so no need to explicitly update them, as your trigger is on before update event.


But in a scenario where parent accounts are updated, it will not work, because at line 68 you are getting a new list by using an SOQL Select query: List<Account> uChild...
This list "uChild" is never updated in this code...
To fix it I think you need to add one line of code: "update uChild;" (just after line 101).

All Answers

kiranmutturukiranmutturu
Are you getting the dubug from line 20 System.debug('No Parent**:');  If not thats the issue....
Abdel-Ali BOULIKAAbdel-Ali BOULIKA
Hello,
I think your code will work fine if child account are updated => the fields will be updated using field values from there parents accounts.
This first scenario will work because I guess the "List<Account>newActList" is coming from Trigger.New, so no need to explicitly update them, as your trigger is on before update event.


But in a scenario where parent accounts are updated, it will not work, because at line 68 you are getting a new list by using an SOQL Select query: List<Account> uChild...
This list "uChild" is never updated in this code...
To fix it I think you need to add one line of code: "update uChild;" (just after line 101).
This was selected as the best answer
KR_ForceKR_Force
Thank you "Abdel-Ali BOULIKA " for the detail explanation,  it worked.