You need to sign in to do that
Don't have an account?

Apex Class Contains Method
Hi, I have a class which loops through an account and then loops through all of the assets within that account to see if the assets have a certain status. I'm trying to use the contains method on the map of accounts but I'm getting the error message that the method doesn't exist or incorrect signature: void contains(boolean) from the type Map.Is there a way to search the status of the assets within an account to see if the status is present.? Thank you.
public class AccountActiveProducts { public AccountActiveProducts(){ List<Account> accountsToUpdate = new List<Account>(); List<Account> oldAccountsToUpdate = new List<Account>(); } // public static void updateAccounts(List<SObject> assetLists, Map <Id, Account> AccountOldMap){ public static void updateAccounts(List<SObject> assetLists){ List<Account> accountsToUpdate = new List<Account>(); Set<String> setAssetsToRemove = new Set<String>(); List<String> listAssetsToRemove = new List<String>(); Set<Id> ParentIds = new Set<Id>(); for(Asset asset:(List<Asset>)assetLists){ ParentIds.add(asset.AccountId); } // for(Asset oldAsset:AccountOldMap){ // } Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, ProductsOwned__c, (Select Id, ProductFamily__c, IsCompetitorProduct, Status from Assets Where (Status != 'Retired' OR Status != 'Obsolete' OR Status != null)) From Account Where Id In: ParentIds]); // Map<Id, Account> mapAccountPurchased = new Map<Id, Account>([Select Id, (Select Id, ProductFamily__c, Status from Assets Where ( Status != null)) From Account Where Id In: ParentIds]); if(Trigger.isInsert || Trigger.isUpdate){ for(Account ac : mapAccount.values()){ List<String> assetList = new List<String>(); Set<String> assetSet = new Set<String>(); Set<String> assetsPurchased = new Set<String>(); List<String> assetValues = new List<String>(); for(Asset accountAsset: ac.assets){ if(accountAsset.Status != 'Retired' || accountAsset.Status != 'Obsolete'|| accountAsset.IsCompetitorProduct != true ){ assetSet.add(accountAsset.ProductFamily__c); } // if(accountAsset.Status == 'Retired' || accountAsset.Status == 'Obsolete' && (!ac.assets.contains(accountAsset.Status == 'Retired') || !ac.assets.contains(accountAsset.Status == 'Obsolete'))){ // if(ac.assets.contains(accountAsset.status == 'Retired' || accountAsset.status == 'Obsolete' ) // if(accountAsset.Status == 'Retired' || accountAsset.Status == 'Obsolete'|| accountAsset.IsCompetitorProduct == true){ ******* This is where I'm trying to search for the status. if((accountAsset.Status == 'Retired' || accountAsset.Status == 'Obsolete'|| accountAsset.IsCompetitorProduct == true) && !(mapAccount.contains(accountAsset.Status == 'Installed' || accountAsset.Status == 'Registered' || accountAsset.Status == 'Shipped') )){ assetSet.remove(accountAsset.ProductFamily__c); } } assetList.addAll(assetSet); assetList.sort(); String productFamily = string.join(assetList,', ' ); if(ac.ProductsOwned__c != productFamily ){ ac.ProductsOwned__c = productFamily; accountsToUpdate.add(ac); } } //End of for loop above if(!accountsToUpdate.isEmpty()){ upsert accountsToUpdate; } } if(Trigger.isDelete){ } } }
AssetPrimaryTrigger AccountActiveProducts
All Answers
From what I see, you're already iterating through each Account and each Asset belonging to a single Account. Is there a reason you want to check the map again?
So I was wondering if maybe using a trigger.oldMap would work in this case to scroll through all of the asset's in the account and take any assets whose status isn't retired or obsolete and roll that product family into the active products field.
From what I see, you're populating assetSet only when the asset has the status you want, so what is the use case of the assetSet.remove line?
Greetings to you!
- You were trying to give condition in contains method but it will take only 'Id' of
- On line number 48, use the below line : - I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha.
In your IF A, change the || to &&.
In your example, an Account has two assets, first one installed and the second one retired, so what should be the result?
Try removing the if line in your helper class and see if it changes anything.
Is it a competitor product?
Also, does it ever work if the active products field is null?
AssetPrimaryTrigger AccountActiveProducts