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
3C3C 

Trigger incorrectly updating records

We have a trigger that is meant to update account records to customer (meaning they have active assets) or location (no active assets). This is the relevant part of the associated class. In the final lines, Type is supposed to be updated to Location only if there are no active assets. However, in testing it is updating to Location whenever we deactivate any asset on an account, even if there are other assets that are still active. What could be causing this?
 
List<Account> accountsToUpdate = new List<Account>();
        for(String accountID: mapAccountToAssets.keySet())
        {
            Account loc = new Account(ID = accountID);
            Boolean hasOnMaintenanceAssets = false;
            Boolean isLocation = false;
            for(Customer_Asset__c ca: mapAccountToAssets.get(accountID))
            {
                if(ca.Account__r.RecordTypeID == AccountServices.recordTypesNameMap.get(Constants.ACCOUNT_RECORD_TYPE_FACILITY).ID)
                    isLocation = true;
                    
                if(ca.Maintenance_Status__c == Constants.STATUS_ON_SUBSCRIPTION && ca.Annual_Maintenance__c > 0)
                {
                    hasOnMaintenanceAssets = true;
                    loc.Type = 'Customer';
                    break;
                }
            }
            
            if(!hasOnMaintenanceAssets)
            {
                if(isLocation)
                    loc.Type = 'Location';
            }
            
            accountsToUpdate.add(loc);
        }
        
        update accountsToUpdate;
        
    }

 
SonamSonam (Salesforce Developers) 
So I understand an account might have multiple customer assets which can be active/deactive but I do not see that we are getting a list of all the assets from a account - I think you should get all the assets linked to an account and didvide them into 2 lists - active & deactive - if the active list has zero - then you should be changing the location else not.

Hoping I've understood the questions correctly..