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
Iswarya SekarIswarya Sekar 

Trigger to count the no of assets based on record type from child accounts

Hi Everyone!

I have a requirement where i cannot find an solution. Can anyone pls help me on this.

Requirement - I have a number field in Account and if this account has 3 child accounts, then the assets related to these 3 accounts should be calculated and updated in parent account field.

Example - Account name - ABC
if ABC has 2 child accounts 
child 1 - has 3 assets varies by record type
child 2 - has 2 assets varies by record type

Then ABC account custom field should be populated with the value as "5" if all 5 assets falls under same record Type
Andrew GAndrew G
The solution will rely on the trigger (or flow) being based on the Asset being edited.
Assuming we go trigger, psuedo code would be:

If Asset is certain record type
Get Account record
Get Parent Account record from Account
Get all Account records with the same Parent Account
With the list of Account Ids, get all the assets with a certain record type and account is the Account Id list
Check the Size of the List
Update the Parent account.

Here is a ticket i have previously responed to that should help flesh out the above
Error: Update parent status based on child statuses - Salesforce Developer Community (https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IGZBQA4)

You could probably apply the same logic to using a trigger flow.

regards 
Andrew
 
Iswarya SekarIswarya Sekar
Hi Andrew

Thanks for your help. But my code doesn't seems to work

trigger RollupAsset on Asset (before insert,before update,after delete, after insert, after update) {
List<id> accIdList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Asset AsV : Trigger.new){
            if(Asv.AccountId!=null){
                accIdList.add(AsV.Account.ParentId);
            }
        }
    }
    if(Trigger.isDelete){
        For(Asset AsV : Trigger.new){
            if(Asv.AccountId!=null){
                accIdList.add(AsV.Account.ParentId);
            }
        }
    }
    
    
    List<Account> accUpdateList = new List<Account>();
    Map<Id,Account> CountMap = new Map<Id,Account>();
    List<aggregateResult> results=[select Count(Id) ,Account.ParentId ParentValue from Asset where Account.parentId=:accIdList and RecordTypeId='012f20000005QG0' group by Account.ParentId];
    
    for (AggregateResult ar : results){
        
        Integer rowvalue = 0;
        
        Id ParentAccountValue = null;
        ParentAccountValue = (Id)ar.get('ParentValue');    
        
        if (!CountMap.containskey(ParentAccountValue)){
            
            For(Account acc : [SELECT id,Count_of_Active_WeatherTRAK_Subscription__c FROM Account WHERE id =: accIdList]){
                if(CountMap.containskey(acc.id)){
                    countMap.get(acc.Id).Count_of_Active_WeatherTRAK_Subscription__c = acc.Assets.Size();
                    accUpdateList.add(countMap.get(acc.Id));
                }
            }
        }
    }
    try{
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
    
}
Andrew GAndrew G
First, when doing a query against a list you must use IN not = (equals). So adjust your QUERY
List<aggregateResult> results=[select Count(Id) ,Account.ParentId ParentValue from Asset where Account.parentId IN :accIdList and RecordTypeId='012f20000005QG0' group by Account.ParentId];
Next question.
Is the record type based on the Asset or the Account?  from your notes, I assumed it is the record type for the asset.  
If so, then the Aggregate Query will be incorrect as the RecordType Id will not match for the Account.

Next, you have a Query inside a For loop - this is bad ju-ju
for (AggregateResult ar : results){
        
        Integer rowvalue = 0;
        
        Id ParentAccountValue = null;
        ParentAccountValue = (Id)ar.get('ParentValue');    
        
        if (!CountMap.containskey(ParentAccountValue)){
            
            For(Account acc : [SELECT id,Count_of_Active_WeatherTRAK_Subscription__c FROM Account WHERE id =: accIdList]){

So,
1. Can you clarify that the Record Type is against the Account or against the Asset?

2. If it is against the Account, then confirm that your query returns some values. Base on the next query for Account to include Count_of_Active_WeatherTRAK_Subscription__c  i suspect the record type is against the Asset.

So, a quick piece of code would look like:
 
trigger RollupAsset on Asset (before insert,before update,after delete, after insert, after update) {
List<id> accIdList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete ){
        For(Asset AsV : Trigger.new){
          if(Asv.RecordTypeId = '012f20000005QG0') {
            if(Asv.AccountId!=null){
                accIdList.add(AsV.Account.ParentId);
            }
          }
        }
    }

if(accIdList.size() > 0 ) {
    
    List<Account> accUpdateList = new List<Account>();

    Map<Id,Account> CountMap = [SELECT id,Count_of_Active_WeatherTRAK_Subscription__c FROM Account WHERE id IN : accIdList];

    List<aggregateResult> results=[select Account.ParentId ParentValue, Count(Id) FROM Asset where Account.parentId IN :accIdList group by Account.ParentId];
    
    for (AggregateResult ar : results){
        ParentAccountValue = (Id)ar.get('ParentValue');    
        
        if (CountMap.containskey(ParentAccountValue)){
           countMap.get(acc.Id).Count_of_Active_WeatherTRAK_Subscription__c = ar.get('expr0');
                    accUpdateList.add(countMap.get(acc.Id));
                }
            }
        }
    }
    try{
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}    
}



Code supplied uncompiled and as-is

but should give you the idea

regards
Andrew