Ask Search:
Louise RamsdenLouise Ramsden 

How do you prevent a contact record from being deleted?

We have a number of contact records in our database that meet certain criteria (user ID not equal to blank). Lets call this type of contact a Master Contact. I need to ensure that these Master contact records cannot be deleted and that the contact ID for this record remains the same.

I also want to ensure that when this Master record is merged with another record that the contact ID of the master contact record is always used and that it is not possible to merge two master contact records. 

Has anyone ever tried to do this and what method did they use?

Hanks in advance!
Best Answer chosen by Louise Ramsden
Nitish SinghalNitish Singhal
Hi Lousie,

There are two things in your question:

1. To prevent deletion of master type of contact records. 
For this problem, I would suggest a very simple apex trigger to be written on contact object on before delete event. Please try creating a trigger with the below code, just a small change at line number 3, replace userId with your field API name. 
trigger preventDeletion on contact (before delete){
for(contact con : trigger.old){
if(con.userId != null){
con.addError('You can not delete these kind of contact records');
}
}

2. To allow merging only if your master type of contact is selected as Master record in merging. 
This will be taken care automatically I think, because you have already prevented deletion of master type of contacts. Because what happens during merging is, merge process tries to delete the slave records during merging, and if your master type of contact will be chosen as slave, SF will try to delete it, and your apex trigger won't allow that. 

Please try, hope this helps !!

Thanks
Nitish

All Answers

Orkhan ShukurovOrkhan Shukurov
Unfortunately you can't prevent deletion via point-and-click. You need to write a trigger (on before delete).
Louise RamsdenLouise Ramsden
Thanks Orkhan! Do you have any advice on how the trigger would work?
Nitish SinghalNitish Singhal
Hi Lousie,

There are two things in your question:

1. To prevent deletion of master type of contact records. 
For this problem, I would suggest a very simple apex trigger to be written on contact object on before delete event. Please try creating a trigger with the below code, just a small change at line number 3, replace userId with your field API name. 
trigger preventDeletion on contact (before delete){
for(contact con : trigger.old){
if(con.userId != null){
con.addError('You can not delete these kind of contact records');
}
}

2. To allow merging only if your master type of contact is selected as Master record in merging. 
This will be taken care automatically I think, because you have already prevented deletion of master type of contacts. Because what happens during merging is, merge process tries to delete the slave records during merging, and if your master type of contact will be chosen as slave, SF will try to delete it, and your apex trigger won't allow that. 

Please try, hope this helps !!

Thanks
Nitish
This was selected as the best answer
Louise RamsdenLouise Ramsden
Thanks Nitish. I will give this a try. 
Nitish SinghalNitish Singhal
please let me know, if you need any help !! But I am almost sure, that this trigger would work for both of your situtations. 

Best Regards
Nitish
Louise RamsdenLouise Ramsden
Thanks Nitish. This is the trigger code I have used. The field is a checkbox called User ID where the result needs to be TRUE for the contact to be a Master. 

trigger preventDeletion on Contact (before delete){
for(contact con : trigger.old){
if(userid != true){
con.addError('You can not delete these kind of contact records');
}
Nitish SinghalNitish Singhal
Please have (con.userId == true).

If this checkbox is true, then code will consider that as master, and won't allow the user to delete that record, by using addError method. 

Please try.

 
Orkhan ShukurovOrkhan Shukurov
+1 Nitish
This should work:
trigger preventDeletion on Contact (before delete){
for(contact con : trigger.old){
if(con.userid__c == True){
con.addError('You can not delete these kind of contact records');
}

 
Louise RamsdenLouise Ramsden
Thank you both. I have copy and pasted Orkhan's answer. I get this error message. 

Error: Compile Error: expecting right curly bracket, found '<EOF>' at line 6 column 0
Jeff MayJeff May
I would not use a trigger for this. I would instead, remove the Delete permissions from your users.  There is no need to delete Contacts, so no one needs to have that permission.
Louise RamsdenLouise Ramsden
Needs 3 closed brackets and it works. Thank you!
Nitish SinghalNitish Singhal
Lousie,

Can you please mark the best answer if this completely works for you.
Louise RamsdenLouise Ramsden
Thanks Nitish. I created and tested this trigger in my sandbox environment as I couldn't create it in the production org. Do you know how I can publish it to the production org?
Nitish SinghalNitish Singhal
Lousie, you need to create a outbound changeset in your sandbox, and you will need a test class as well in order to deploy the trigger to your Production environment. 

I would suggest you take help from some developer to get the test class done. 

Once done create a outbound changeset including this trigger and your test class, with the userId field as well, and upload it to Production. 

Thanks
Nitish