+ 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/
minion rolsenminion rolsen
so and i want see a answer  for  this  questen, nice questen
Plague inc download  
sunny ahujasunny ahuja

Well, You have helped me buy posting this question and I want to thank you for this!

Plague inc download (https://love4apk.com/plague-inc-apk/)

John kevin 1John kevin 1
bigg boss voting (https://bigg-boss-vote.in) This the very first reality show of Sony entertainment television call Bigg Boss was hosted by Arshad Warsi. Bigg Boss was started on Sony entertainment television but now it is being streamed on Colors TV. You can say that Bigg Boss follows the Big Brother format, which watches first developed by Endemol in the Netherlands. bigg boss tamil vote (https://bigg-boss-vote.in/bigg-boss-tamil-vote)
Cash DavidsonCash Davidson
Thank you a lot for the solution! My details of dealing can be found here - apknature.com
Cash DavidsonCash Davidson
This also can be used in such games like plague inc on android (https://appmirror.net/games/strategy/plague-inc/) devices
Khakan KhanKhakan Khan
I have seen these informations and the method is really working.
Advanced pens with 3d technology (https://techypes.com/best-3d-pens/" target="_blank) helps in achieving best creativity
Khakan KhanKhakan Khan
We should also control the way Nursing Laptops (https://besticious.com/best-laptops-for-nursing-students/) are being effected.
bernard dalebernard dale
.I just wanted to thank you for this, its much helpful for my site, it helped me build my pokemon go spoofer pokemon go mod apk (https://gameorbiter.com/pokemon-go-mod-apk/)