You need to sign in to do that
Don't have an account?
Rohan S
Trigger not getting executed properly
I'm wrtting a trigger which restricts an account to be deleted if it has opportunities. Below is the Apex class & trigger.
Apex Class:
public class RestrictAccountDeleteClass
{
public static void RestrictAccountDelete(list<account>AccList)
{
list<account> OldAccList = new list<account>();
OldAccList = [SELECT Id, (SELECT Id FROM Opportunities ) FROM Account WHERE Id IN : AccList];
for(Account a : OldAccList)
{
if(a.Opportunities.size()!=0)
{
a.addError('Cannot delete account as it has associated opportunities');
}
}
}
}
Trigger:
trigger RestrictAccountDeleteTrigger on Account (before delete) {
if(trigger.isBefore==true && trigger.isDelete==true){
RestrictAccountDeleteClass.RestrictAccountDelete(trigger.old);
}
}
When I try to delete a record, it gives the following error: Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger RestrictAccountDeleteTrigger caused an unexpected exception, contact your administrator: RestrictAccountDeleteTrigger: execution of BeforeDelete caused by: System.FinalException: SObject row does not allow errors: Class.RestrictAccountDeleteClass.RestrictAccountDelete: line 11, column 1".
Can someone please let me know where I am going wrong?
Apex Class:
public class RestrictAccountDeleteClass
{
public static void RestrictAccountDelete(list<account>AccList)
{
list<account> OldAccList = new list<account>();
OldAccList = [SELECT Id, (SELECT Id FROM Opportunities ) FROM Account WHERE Id IN : AccList];
for(Account a : OldAccList)
{
if(a.Opportunities.size()!=0)
{
a.addError('Cannot delete account as it has associated opportunities');
}
}
}
}
Trigger:
trigger RestrictAccountDeleteTrigger on Account (before delete) {
if(trigger.isBefore==true && trigger.isDelete==true){
RestrictAccountDeleteClass.RestrictAccountDelete(trigger.old);
}
}
When I try to delete a record, it gives the following error: Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger RestrictAccountDeleteTrigger caused an unexpected exception, contact your administrator: RestrictAccountDeleteTrigger: execution of BeforeDelete caused by: System.FinalException: SObject row does not allow errors: Class.RestrictAccountDeleteClass.RestrictAccountDelete: line 11, column 1".
Can someone please let me know where I am going wrong?
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Handler:
Trigger:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
All Answers
Please try this code it may help you
Thank You
www.nubeselite.com
Developement | Training | Consulting
Please mark this as solution if your problem resolved.
Greetings to you!
Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
Handler:
Trigger:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
Try this code.
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
www.kdeepali.com