function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rob Koch 1Rob Koch 1 

Apex Trigger to Update Account on Asset Delete

Hello, I am looking to create a trigger that will update an account field to "Null" or "0" when a child asset is deleted. The two account fields are Max Advisor Count, and Max Mortgage Advisor Count. When adding an asset, depending on the type, it will add a quantity of "1" to either the Max Advisor Count or the Max Mortgage Advisor Count

I set up Process Builder to update those counts when an asset record is created or changed. But it looks like I will need a trigger to set either of those counts to 0 when an asset record is deleted.

All I need is for the trigger to fire after an asset is deleted. Any suggestions are welcome, thank you!!!
Best Answer chosen by Rob Koch 1
Abhishek BansalAbhishek Bansal
Hi Rob,

Your trigger will look something like below:
trigger updateAccount on Asset (after delete){
	Set<Id> accIds = new Set<Id>();
	
	for(Asset ast : trigger.old){
		if(ast.AccountId != null){
			accIds.add(ast.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Max_Advisor_Count__c, Max_Mortgage_Advisor_Count__c From Account where Id IN :accIds]);
	
	List<Account> listOfAcc = new List<Account>();
	for(Asset ast : trigger.old){
		if(ast.AccountId != null && mapOfAccounts.containsKey(ast.AccountId)){
			if(mapOfAccounts.get(ast.AccountId).Max_Advisor_Count__c > 0){
				mapOfAccounts.get(ast.AccountId).Max_Advisor_Count__c --;
				listOfAcc.add(mapOfAccounts.get(ast.AccountId));
			}
		}
	}
	if(listOfAcc.size() > 0){
		update listOfAcc;
	}
}
//Please take care of the API name of the fields used in the code.

However the code needs to be modify based on your requirement. Please let me know if you need any further help on this.

Thanks,
Abhishek Bansal

All Answers

Abhishek BansalAbhishek Bansal
Hi Rob,

Your trigger will look something like below:
trigger updateAccount on Asset (after delete){
	Set<Id> accIds = new Set<Id>();
	
	for(Asset ast : trigger.old){
		if(ast.AccountId != null){
			accIds.add(ast.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Max_Advisor_Count__c, Max_Mortgage_Advisor_Count__c From Account where Id IN :accIds]);
	
	List<Account> listOfAcc = new List<Account>();
	for(Asset ast : trigger.old){
		if(ast.AccountId != null && mapOfAccounts.containsKey(ast.AccountId)){
			if(mapOfAccounts.get(ast.AccountId).Max_Advisor_Count__c > 0){
				mapOfAccounts.get(ast.AccountId).Max_Advisor_Count__c --;
				listOfAcc.add(mapOfAccounts.get(ast.AccountId));
			}
		}
	}
	if(listOfAcc.size() > 0){
		update listOfAcc;
	}
}
//Please take care of the API name of the fields used in the code.

However the code needs to be modify based on your requirement. Please let me know if you need any further help on this.

Thanks,
Abhishek Bansal
This was selected as the best answer
Rob Koch 1Rob Koch 1
Hello Abhishek. Thank you for that helpful snippet! I will test it in our sandbox and let you know how it worked.
Rob Koch 1Rob Koch 1
Hello Abhishek, I tested it and it is working to update the max advisor counts. However, my logic might be off a bit. When I deleted a Mortgage related asset related, it set the Max Advisor Count to 0. It needs to set the Max Mortgage Advisor Count to 0. 

So when there is only one related Asset that contains the name "Figlo Advisor", quantity of 1 and it is deleted, set Max Advisor Count to 0. Otherwise, reduce the Max Advisor Count by 1.

When there is only one related Asset that contains the name "Figlo Hypotheken," quantity of 1 and it is deleted, set Max Mortgage Advisor Count to 0. Otherwise reduce the Max Mortgage Advisor Count by 1.

So this is an excellent start. All I need now is help with the logic to update the counts according to the above conditions.

The fields involved are:

Asset Fields:
Asset Name
Asset Quantity

Account Fields:
Max_Advisor_Count__c 
Max_Mortgage_Advisor_Count__c

Thank you again! I was happy to see that the underlying trigger is working.

 
User-added image
Rob Koch 1Rob Koch 1
Can you assist with the last part of my question?
Abhishek BansalAbhishek Bansal
Hi Rob,

Please find below your required trigger code:
trigger updateAccount on Asset (after delete){
	Set<Id> accIds = new Set<Id>();
	
	for(Asset ast : trigger.old){
		if(ast.AccountId != null){
			accIds.add(ast.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select Max_Advisor_Count__c , Max_Mortgage_Advisor_Count__c From Account where Id IN :accIds]);
	
	Set<Account> listOfAcc = new Set<Account>();
	for(Asset ast : trigger.old){
		if(ast.Name.contains('Figlo Advisor')){
			if(ast.Quantity == 1){
				if(ast.AccountId != null && mapOfAccounts.containsKey(ast.AccountId)){
					mapOfAccounts.get(ast.AccountId).Max_Advisor_Count__c = 0;
					listOfAcc.add(mapOfAccounts.get(ast.AccountId));
				}
			}
			else{
				if(ast.AccountId != null && mapOfAccounts.containsKey(ast.AccountId) && mapOfAccounts.get(ast.AccountId).Max_Advisor_Count__c > 0){
					mapOfAccounts.get(ast.AccountId).Max_Advisor_Count__c --;
					listOfAcc.add(mapOfAccounts.get(ast.AccountId));
				}
			}
		}
		else if(ast.Name.contains('Figlo Hypotheken')){
			if(ast.Quantity == 1){
				if(ast.AccountId != null && mapOfAccounts.containsKey(ast.AccountId)){
					mapOfAccounts.get(ast.AccountId).Max_Mortgage_Advisor_Count__c = 0;
					listOfAcc.add(mapOfAccounts.get(ast.AccountId));
				}
			}
			else{
				if(ast.AccountId != null && mapOfAccounts.containsKey(ast.AccountId) && mapOfAccounts.get(ast.AccountId).Max_Mortgage_Advisor_Count__c > 0){
					mapOfAccounts.get(ast.AccountId).Max_Mortgage_Advisor_Count__c --;
					listOfAcc.add(mapOfAccounts.get(ast.AccountId));
				}
			}
		}
	}
	if(listOfAcc.size() > 0){
		List<Account> updateAccList = new List<Account>();
		updateAccList.addAll(listOfAcc);
		update updateAccList;
	}
}

Let me know if you face any issues with this.

Thanks,
Abhishek Bansal.​