+ Start a Discussion
farah sheriffarah sherif 

I need help with a trigger

the trigger should be on account to check the assets and if ALL Assets where product family = "bla bla "  and their status was "Canceled" the trigger should update bla bla Account stage (API Name = Account_Status__c )  to "Cancel".
Best Answer chosen by farah sherif
Abdul KhatriAbdul Khatri
Can you try the below
 
trigger AccountUpdateFromAssetProduct on Asset (after insert, after update) {
    
    List<Asset> assetList = Trigger.new;

	List<Id> idAccountList = new List<Id>();    
    for(Asset asset : assetList) {
        
        if(Trigger.isUpdate && asset.Product2.Family == Trigger.oldMap.get(asset.Id).Product2.Family && asset.Status == Trigger.oldMap.get(asset.Id).Status) continue;
        
        if(asset.AccountId == null) continue;
        
        idAccountList.add(asset.AccountId);
    }
    
    if(idAccountList.isEmpty()) return;
    
    List<Account> acctList = [SELECT Id, (SELECT Id FROM Assets WHERE Product2.Family = 'bla bla' AND Status = 'Canceled') FROM Account LIMIT 50];
    
    for(Account acct : acctList)
    {
        if(acct.Assets.isEmpty()) continue;
        
        acct.Account_Status__c = 'Cancel';
    }

    update acctList;
}

 

All Answers

Abdul KhatriAbdul Khatri
Can you try the below
 
trigger AccountUpdateFromAssetProduct on Asset (after insert, after update) {
    
    List<Asset> assetList = Trigger.new;

	List<Id> idAccountList = new List<Id>();    
    for(Asset asset : assetList) {
        
        if(Trigger.isUpdate && asset.Product2.Family == Trigger.oldMap.get(asset.Id).Product2.Family && asset.Status == Trigger.oldMap.get(asset.Id).Status) continue;
        
        if(asset.AccountId == null) continue;
        
        idAccountList.add(asset.AccountId);
    }
    
    if(idAccountList.isEmpty()) return;
    
    List<Account> acctList = [SELECT Id, (SELECT Id FROM Assets WHERE Product2.Family = 'bla bla' AND Status = 'Canceled') FROM Account LIMIT 50];
    
    for(Account acct : acctList)
    {
        if(acct.Assets.isEmpty()) continue;
        
        acct.Account_Status__c = 'Cancel';
    }

    update acctList;
}

 
This was selected as the best answer
farah sheriffarah sherif
thank you so much but I have a question

what does this line of code do ?  
 List<Asset> assetList = Trigger.new;


and can I have your email for more questions?
Abdul KhatriAbdul Khatri
Thanks Please remove that LIMIT 50 in the SOQL.
farah sheriffarah sherif
 what does this line of code do ?

if(Trigger.isUpdate && asset.Product2.Family == Trigger.oldMap.get(asset.Id).Product2.Family && asset.Status == Trigger.oldMap.get(asset.Id).Status) continue;
Abdul KhatriAbdul Khatri
It is preventing running the trigger even if you make updates apart from those fields on asset. You goal is to make the trigger run only in the cases you want.
beetv appbeetv app
Then what should be the limit if i remove LIMIT 50 in the SOQL? any update on this?
Thank you in advance.
Developer at https://beetvapp.com/