+ Start a Discussion
farah sheriffarah sherif 

i need help with this trigger because it's giving me an error

the trigger should be on account to check the assets and if ALL the Assets belonging to product family = "bla bla " had their status "Canceled" the trigger should update bla bla Account stage (API Name = Account_Status__c ) to "Cancel".

below is a trigger I wrote but it doesn't work

trigger cancelAsset on Account (after update) {

    
    List<Asset> suite = [Select Id ,Status from Asset where Product_Family__c = '360 Suite']; 
    List<Asset> tech = [Select Id ,Status from Asset where Product_Family__c = 'Ad Tech']; 
    for(Account acc:Trigger.new){
        
        Integer z=0;
        for(Integer i=0 ; i<= suite.size() ;i++){
            if(suite[i].Status == 'Canceled'){
              z++;
            }
        }
        
        if (z == suite.size()){
            acc.Account_Status__c = 'Cancel';
        }
        update acc;
    }
}

 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Farah

You are updating the same account record so you need to implement your code on before update and skip this external update statement.

Cheers!!!
Raj VakatiRaj Vakati
Try this code
 
trigger cancelAsset on Account (before update) {

    
    List<Asset> suite = [Select Id ,Status from Asset where Product_Family__c = '360 Suite']; 
    List<Asset> tech = [Select Id ,Status from Asset where Product_Family__c = 'Ad Tech']; 
    for(Account acc:Trigger.new){
        
        Integer z=0;
        for(Integer i=0 ; i<= suite.size() ;i++){
            if(suite[i].Status == 'Canceled'){
              z++;
            }
        }
        
        if (z == suite.size()){
            acc.Account_Status__c = 'Cancel';
        }
       // update acc;
    }
}

 
farah sheriffarah sherif
I tried it but it didn't work
SFDC Prime SquadSFDC Prime Squad
Hi Farah,

The logic should be bulkied and follow the best practices.
Try the below code:

trigger cancelAsset on Account (before update) {

    
    //List<Asset> suite = [Select Id ,Status from Asset where Product_Family__c = '360 Suite'];     // This query will be retrieve all asset records from database. So we should use filters and optimise the query by getting assets only related to accounts being processed
    
    List<Asset> suite = new List<Asset>();
    set<ID> accIds = new set<Id>();
    map<Id,Boolean> accIdStatusMap = new map<Id,Boolean>();
    
    for(Account acc:Trigger.new){
        accIds.add(acc.Id);
    }
    
    if(accIds != null && accIds.size() > 0){
        suite = [select Id ,Status from Asset where Product_Family__c = '360 Suite' and AccountID in: accIds];
    }
    
    if(suite != null && suite.size() > 0){
        for(Asset assetRec : suite){
            if(assetRec.AccountId != null){
                accIdStatusMap.put(assetRec.AccountId,true);
            }
            if(assetRec.Status != 'Canceled'){
                accIdStatusMap.put(assetRec.AccountId,false);
            }
            else if(assetRec.Status == 'Canceled' && accIdStatusMap.conatinsKey(assetRec.AccountId) && accIdStatusMap.get(assetRec.AccountId)){
                accIdStatusMap.put(assetRec.AccountId,true);
            }
        }
    }
    
    for(Account acc:Trigger.new){
        if(accIdStatusMap.conatinsKey(acc.Id)){
            acc.Account_Status__c = 'Cancel';
        }
    }  
}
farah sheriffarah sherif
thank you for your help
your code is giving me this error
Method does not exist or incorrect signature: void conatinsKey(Id) from the type Map<Id,Boolean>
SFDC Prime SquadSFDC Prime Squad
Hi Farah,
It is incorrectly spelt as conatinskey.
It should be containsKey
farah sheriffarah sherif
aha okay .. can I have your email please? for further questions
farah sheriffarah sherif
SFDC PRime .. the code is giving me this erro when I try to cancel the asset

dlrs_AssetTrigger: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0011900000epHTZAA2; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, cancelAsset: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Asset.AccountId Trigger.cancelAsset: line 20, column 1: [] Class.dlrs.RollupService.Updater.updateRecords: line 1315, column 1 Class.dlrs.RollupService.UpdateWithoutSharing.updateRecords: line 1358, column 1 Class.dlrs.RollupService.updateRecords: line 1286, column 1 Class.dlrs.RollupService.handleRollups: line 892, column 1 Class.dlrs.RollupService.triggerHandler: line 311, column 1 Trigger.dlrs_AssetTrigger: line 7, column 1
SFDC Prime SquadSFDC Prime Squad
Hi Farah,
The error that you are encountering is due to package trigger.
You need to investigate the issue related to package trigger and try to deactivate the package trigger and see
farah sheriffarah sherif
hello SFDS PRime squad

 if(assetRec.AccountId != null){
                accIdStatusMap.put(assetRec.AccountId,true);
            }

what does this line of code do
Raju yadavRaju yadav
Hi farah sherif,
you Should update your code as follow
trigger cancelAsset on Account (before update) {

    
    List<Asset> suite = [Select Id ,Status from Asset where Product_Family__c = '360 Suite']; 
    List<Asset> tech = [Select Id ,Status from Asset where Product_Family__c = 'Ad Tech']; 
    for(Account acc:Trigger.new){
        for(Integer i=0 ; i<= suite.size() ;i++){
            if(suite[i].Status == 'Canceled'){
              acc.Account_Status__c = 'Cancel';
            }
       }
    }    
 }

Thanks,