You need to sign in to do that
Don't have an account?
ryyh
Restrict delete of master record if it has children
I recall something along time ago were you could stop the deleting of a record if it has children (in a master or lookup relationship).
The method used validation rules instead of a trigger.
Anyone know how to do this?
Thanks
Hi ryyh,
When creating a new lookup relationship you have an option to stop the deletion of the lookup record that's part of a lookup relationship.
Is this what you were looking for?
There's a way to do it with i believe rollup fields & validation rules. It was in the Salesforce Developer Certification videos i saw like 5 years ago but can't find it now
Hi ryyh,
Validation rule is used to check the data is correctly entered before creation of a record or updation.
So, it cannot be applied while deletion. But, You can achieve this using trigger.
Try the following trigger example,
trigger preventDeletion on Parent__c(before delete){
for(Parent__c pa : Trigger.Old){
Child__c chlist = [select id, parent__c from child where parent__c =: pa.id]; //list to hold child matches the parent
if(chlist.size() > 0){
pa.adderror('Child record is refering this record...So, you cannot delete it...!');
}
}
}
The above trigger will execute while you try to delete a record that has a child records exist.
Hopw this will help you...!
Hi ryyh,
Take a look at this: Useful validation formulas
This includes a validation to stop the deletion of child records, this may help (see page 26 and 27).
Hope this helps!
for (Account a:trigger.old)
{
list<contact> c = [select AccountID from contact where AccountID =:a.id];
if (c.size()>0){
a.adderror('Account is associated with child records');
}
}
}
Thanks!