Here is the simplest trigger to restrict deletion for any particular User or Profile
Steps to create a trigger in Salesforce:
Step 1 – Login into your Salesforce org and click on the gear icon located in the top right corner of the org. It will open a dropdown, you need to click on the ‘Developer Console’.
It will open the Developer Console for you. Now click on the ‘File → New → Apex Trigger’ as shown below.
It will prompt you to enter a name and select the object on which you want to run the trigger and then click on ‘Submit’ to create a trigger. After the record is created, it will open the file in the same window. An example is shown below.
There are two examples shown:
- Restrict a Profile from deleting a Record.
2. Restrict a user from deleting a Record.
After writing the code, just save the record by pressing ‘Ctrl + S’.
Step 2 – Open a contact record in your Salesforce org. Click on the inverted triangle icon located on the top-right of the contact record. It will open a dropdown, click on the ‘Delete’.
It will prompt you to confirm the delete action by clicking on the ‘Delete’ button.
As soon as you click on the button, you will see the error message which we wrote in our trigger class that ‘You are not allowed to delete Contact records!’.
Atul Gupta is CloudVandana’s founder, an 8X Salesforce Certified, working with globally situated businesses in creating Custom Salesforce Solutions.
A strong, dynamic, and accomplished leader, as Director at Atul Gupta, guides all the aspects of CloudVandana Salesforce Implementation Team, Analytics, and Information Technology functions.