You need to sign in to do that
Don't have an account?
Joshua Graham 13
How to use a sum function in a trigger
What I am trying to do is look at the asset related list under the account object. I want to sum up the Price field (MRR) for each asset that shows a status of "Active / Live". If it sums up to <$1000 I want it to set a field called "Customer Type" to Bronze <1000, and if the sum of the assets with that Active / Live status is greater than $1000 but less than $2000, I want the Customer Type field to set to Silver....and so on. I am really new to coding and am not sure how to proceed. I was able to get the following code to count the number of assets and populate a custom field I made with that number. I figured I could some how make this sum instead of count and then use if statements to update the Customer Type field based on the sum of that field. Please advise what the best way of accomplishing this?
trigger trgr_Customer_Tier_AutoUpdate on Account (before update) {
Map <Id,integer> assetCount = new Map<Id,integer>();
for(Account a:[SELECT Id,(select Id from Assets) from Account where Id IN:Trigger.New]){
integer count = 0;
for(Asset asset:a.Assets){
count++;
}
assetCount.put(a.Id, count);
}
for(Account account:Trigger.New){
account.Asset_Count__c=assetCount.get(account.Id);
}
}
trigger trgr_Customer_Tier_AutoUpdate on Account (before update) {
Map <Id,integer> assetCount = new Map<Id,integer>();
for(Account a:[SELECT Id,(select Id from Assets) from Account where Id IN:Trigger.New]){
integer count = 0;
for(Asset asset:a.Assets){
count++;
}
assetCount.put(a.Id, count);
}
for(Account account:Trigger.New){
account.Asset_Count__c=assetCount.get(account.Id);
}
}
All Answers
Hi Joshua Graham,
Try below code, this would work for you. if not let me know.
change api names according to ur api names
ping m eif u hav any issues.
@pavanthetech@gmail.com