You need to sign in to do that
Don't have an account?
Kenn K.
Prevent Parent Account to be deleted if it has a child account
I am trying to figure out the best way to prevent users from deleting and Account if it has a child account.
So the scenario will be the users will have to delete all the child accounts first before they can delete the parent.
Any guidance will be highly appreciated.
Thanks.
Sample code which should help you just replace the objectname with Account
trigger onParentObjectDelete on CustomObject__c (before delete){
List<Id> idsToQuery = new List<Id>{};
for(CustomObject__c a: Trigger.new){
idsToQuery.add(a.id);
}
//query all child records where parent ids were deleted
ChildObject__c[] objsToDelete = [select id from ChildObject__c where ParentId__c IN :idsToQuery];
delete objsToDelete; //perform delete statement
}