+ Start a Discussion
farah sheriffarah sherif 

Guys I need help urgently with this trigger PLEASE

We need a trigger on the asset object on update to check all assets.

 If on ALL Assets belonging to product family = "360 Suite" had their  Status = "Canceled" , then on Account, update the 360 Account stage (API Name = Account_Status__c )  to "Cancel".

please if anyone can help with this one I would be so grateful
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Farah

Please find below pseudo code:

TRIGGER_ON_ASSET(AFTER_UPDATE) { 
Set <  AccountId > setOfAcctountIds = new Set < Id >();
for(Asset a : triggerNew) {
if(a.PRODUCT_FAMILY == "360 Suite")
setOfAcctountIds.add(a.accountId);
}
Map<ID,List<Asset>> mapOfAccoutnIdVsAsset = new Map<ID,List<Asset>>();
for(Asset ast :[SELECT Id,...........FROM Asset WHERE PRODUCT_FAMILY = "360....." AND ACCOUNTID IN :setOfAcctountIds]){
if(!mapOfAccoutnIdVsAsset.containskey(ast .AccoutnId))
  mapOfAccoutnIdVsAsset.put(ast .AccoutnId,new List<ASSET>());
mapOfAccoutnIdVsAsset .get(ast .AccoutnId).add(ast);
}
List<Account> acctoupdate = new List <Account>();
for(Id accid : mapOfAccoutnIdVsAsset .keySet()){
Account ac = new Account(Id = accid );
boolean ntcancelledrecord = false;
for(Asset a : mapOfAccoutnIdVsAsset .get(accid )){
if(a.STATUS != 'CANCELLED')
{
 ntcancelledrecord  = true;
break;
}
}
if(!ntcancelledrecord ){
ac.STATUS="Cancel"
acctoupdate .add(ac);
}
}
if(!acctoupdate.isEmpty())
    update acctoupdate;


Cheers!!!
Mohd. KamranMohd. Kamran
Hi farah,

Hoping below code would solve your problem...

trigger updateAccountStatusToCancel on Asset (after update) {
    Set<Id> setAccountIds = new Set<Id>();    
    
    for(Asset as : trigger.new){

        If(as.ProductFamily == '360 Suite' && as.Status == 'Canceled'){

          setAccountIds.add(as.AccountId);

        }
    } 
    
    List<Account> lstAccounts = new List<Account>([SELECT id , Account_Status__c FROM Account WHERE Id IN: setAccountIds]);

    for(Account acc : lstAccounts){
        acc.Account_Status__c = 'Cancel';
    }   
    
    update lstAccounts;
}

Regards
farah sheriffarah sherif
guys I need to use a condition before writhing the code .. that if the status of the new trigger is Cancelled and the ols one is ! canceled then do all this .. how can I write it?
farah sheriffarah sherif
hello Syed Insha Jawaid 2

can I have your email for further questions?
farah sheriffarah sherif
Dear Syed can you please explain the below statements


for(Asset ast :[SELECT Id FROM Asset WHERE Product_Family__c = '360 Suite' AND AccountId IN :setOfAccountIds]){
            
            if(!mapOfAccountIdVsAsset.containskey(ast .AccountId))
              mapOfAccountIdVsAsset.put(ast.AccountId,new List<Asset>());
            mapOfAccountIdVsAsset .get(ast.AccountId).add(ast);
        }
nitin sharma 356nitin sharma 356
You can solve your issue in 2 minutes if you do it through process builder
Syed Insha Jawaid 2Syed Insha Jawaid 2

Hi Farah

The explanation for statements:
1. for(Asset ast :[SELECT Id FROM Asset WHERE Product_Family__c = '360 Suite' AND AccountId IN :setOfAccountIds]) --> Query the Asset record which have product family as '360 Suite' and there Account Id contain in the set.
2.   if(!mapOfAccountIdVsAsset.containskey(ast .AccountId))
              mapOfAccountIdVsAsset.put(ast.AccountId,new List<Asset>());
            mapOfAccountIdVsAsset .get(ast.AccountId).add(ast);
        } --->
If the Map doesnt contain the Account Id and its Asset record then it is added to the map.

Cheers!!!!

Syed Insha Jawaid 2Syed Insha Jawaid 2
My Linkedin : https://www.linkedin.com/in/insha-jawaid-92a75096/