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

Custom Validation errors while saving record(s) on Delete - after deployment of APEX Trigger
Hi I get this error message while trying to delete a setup from main account which has multiple setups. Main idea is that this would go through all the main account setups even when one of the setups is deleted. I would appreciate if you could give me some guidance.
trigger SetupChangeTechnologyListCheck on Setup__c (after insert, after update, after delete)
{
for(Setup__c s : trigger.new)
{
Account acc = [SELECT Name, Service_Type_List__c FROM Account WHERE RecordType.Name = 'Main Account' AND Name = :s.Main_Account__c];
UpdateMainAccountTechnologyList techList = new UpdateMainAccountTechnologyList();
techList.UpdateList(acc);
}
}
After providing you with a quick answer, I realized that you are going to have other problems with your trigger as written. Notably, you have a SOQL query inside the for loop. This is not bulk-safe, as DML of 100+ records will cause you to hit the limit on SOQL queries.
I have refactored your trigger to eliminate this problem. I hope this is helpful.
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
All Answers
Hello,
The context variable Trigger.new is only available in "after insert" and "after update" triggers. Check out the "Trigger context variables" section in the Apex Developer's Guide. You probably want to use System.Trigger.old for the "after delete".
The easy way to fix this is to change your for loop to this:
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
After providing you with a quick answer, I realized that you are going to have other problems with your trigger as written. Notably, you have a SOQL query inside the for loop. This is not bulk-safe, as DML of 100+ records will cause you to hit the limit on SOQL queries.
I have refactored your trigger to eliminate this problem. I hope this is helpful.
If this helps, please mark it as a solution, and give kudos (click on the star) if you think I deserve them. Thanks!
-Glyn Anderson
Certified Salesforce Developer | Certified Salesforce Administrator
BTW: If you make the method 'UpdateList' a static method, then you can call it without the constructor call:
-Glyn
Is Main_Account__c really a String field? Or is it a Lookup field?
-Glyn
If Main_Account__c is actually a Lookup field (and if UpdateList becomes a static method), then the code becomes this:
-Glyn