You need to sign in to do that
Don't have an account?
KR_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?
- public static void AccountUltimateRevenue(Map<ID,Account> newActMap,Map<ID,Account> oldActMap,List<Account>newActList){
- List<Account>actList=new List<Account>();
- List<Account>aList;
- Set<ID>parentActList=new Set<ID>();
- List<Account>uParents=new List<Account>();
- Map<ID, List<Account>> uActListMap = new Map<ID, List<Account>>();
- for(Account a:newActList)
- {
- Account a1=oldActMap.get(a.id);
- // To check whether Parent Account was updated and not null
- if((newActMap.get(a.Id).parentID!=oldActMap.get(a.Id).parentID && newActMap.get(a.Id).parentID<>null))
- {
- ActList.add(a);
- parentActList.add(newActMap.get(a.Id).parentID);
- System.debug('old+++++++++'+a1.parentID+'new++++++++++++++++'+a.parentID);
- System.debug('ActList+++++++++'+ActList);
- }
- else
- if(newActMap.get(a.Id).parentID==null)
- { System.debug('No Parent**:');
- uParents.add(a);
- }
- }
- // Checking if Parent Account list not null pulling all corresponding Ultimate parent revenue and Ultimate parent vertical values from parent accounts.
- if(parentActList!= null && parentActList.size()>0){
- //system.debug( 'Parent Account list:'+parentActList);
- map<Id, Account> uActMap=new map<Id, Account>([select Id,AnnualRevenue,OS_Industry_Sector__c, Ultimate_Parent_Vertical__c, Ultimate_Parent_Revenue__c,
- ParentId, Parent.OS_Industry_Sector__c, Parent.AnnualRevenue,
- Parent.ParentId, Parent.Parent.OS_Industry_Sector__c, Parent.Parent.AnnualRevenue,
- Parent.Parent.ParentId, Parent.Parent.Parent.OS_Industry_Sector__c, Parent.Parent.Parent.AnnualRevenue,
- Parent.Parent.Parent.ParentId, Parent.Parent.Parent.Parent.OS_Industry_Sector__c, Parent.Parent.Parent.Parent.AnnualRevenue
- from Account where ID IN :parentActList]);
- for(Account a: newActList)
- {
- Account ParentAccount = uActMap.get(a.ParentId);
- System.debug('Parent ID**:'+ a.ParentId);
- if (ParentAccount.ParentId ==null){
- System.debug('Condition 1**:');
- a.Ultimate_Parent_Vertical__c = ParentAccount.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = ParentAccount.AnnualRevenue;
- }
- else if (ParentAccount.Parent.ParentId ==null){
- System.debug('Condition 2**:');
- a.Ultimate_Parent_Vertical__c = ParentAccount.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = ParentAccount.Parent.AnnualRevenue;
- }
- else if (ParentAccount.Parent.Parent.ParentId ==null){
- System.debug('Condition 3**:');
- a.Ultimate_Parent_Vertical__c = ParentAccount.Parent.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = ParentAccount.Parent.Parent.AnnualRevenue;
- }
- else if (ParentAccount.Parent.Parent.Parent.ParentId ==null){
- System.debug('Condition 4**:');
- a.Ultimate_Parent_Vertical__c = ParentAccount.Parent.Parent.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = ParentAccount.Parent.Parent.Parent.AnnualRevenue;
- }
- else if (ParentAccount.Parent.Parent.Parent.Parent.ParentId ==null){
- System.debug('Condition 5**:');
- a.Ultimate_Parent_Vertical__c = ParentAccount.Parent.Parent.Parent.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = ParentAccount.Parent.Parent.Parent.Parent.AnnualRevenue;
- }
- }
- }
- if(uParents.size()>0)
- {
- List<Account> uChild=new List<Account>([select Id,AnnualRevenue,OS_Industry_Sector__c, Ultimate_Parent_Vertical__c, Ultimate_Parent_Revenue__c,
- ParentId, Parent.OS_Industry_Sector__c, Parent.AnnualRevenue,
- Parent.ParentId, Parent.Parent.OS_Industry_Sector__c, Parent.Parent.AnnualRevenue,
- Parent.Parent.ParentId, Parent.Parent.Parent.OS_Industry_Sector__c, Parent.Parent.Parent.AnnualRevenue,
- 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]);
- for(Account a: uChild)
- {
- if (a.ParentId ==null){
- System.debug('Condition 6**:');
- a.Ultimate_Parent_Vertical__c = a.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = a.AnnualRevenue;
- }
- else if (a.Parent.ParentId ==null){
- System.debug('Condition 7**:'+a.Parent.OS_Industry_Sector__c+a.Parent.AnnualRevenue);
- //a.Ultimate_Parent_Vertical__c = a.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Vertical__c = a.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = a.Parent.AnnualRevenue;
- }
- else if (a.Parent.Parent.ParentId ==null){
- System.debug('Condition 8**:');
- a.Ultimate_Parent_Vertical__c = a.Parent.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = a.Parent.Parent.AnnualRevenue;
- }
- else if (a.Parent.Parent.Parent.ParentId ==null){
- System.debug('Condition 9**:');
- a.Ultimate_Parent_Vertical__c = a.Parent.Parent.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = a.Parent.Parent.Parent.AnnualRevenue;
- }
- else if (a.Parent.Parent.Parent.Parent.ParentId ==null){
- System.debug('Condition 10**:');
- a.Ultimate_Parent_Vertical__c = a.Parent.Parent.Parent.Parent.OS_Industry_Sector__c;
- a.Ultimate_Parent_Revenue__c = a.Parent.Parent.Parent.Parent.AnnualRevenue;
- }
- }
- }
- }
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
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).