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’.
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2021/06/A1.png?resize=1024%2C214)
It will open the Developer Console for you. Now click on the ‘File → New → Apex Trigger’ as shown below.
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2021/06/A2.png?resize=1024%2C317)
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.
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2021/06/A3.png?resize=441%2C299)
There are two examples shown:
- Restrict a Profile from deleting a Record.
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2021/06/A4a.png?resize=1024%2C241)
2. Restrict a user from deleting a Record.
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2021/06/A4b.png?resize=1024%2C224)
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’.
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2021/06/A5.png?resize=1024%2C322)
It will prompt you to confirm the delete action by clicking on the ‘Delete’ button.
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2021/06/A6.png?resize=843%2C289)
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!’.
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2021/06/A7.png?resize=1024%2C507)
![](https://i0.wp.com/cloudvandana.com/wp-content/uploads/2020/04/atul.png?resize=100%2C100)
Atul Gupta is CloudVandana’s founder and an 8X Salesforce Certified Professional who works with globally situated businesses to create Custom Salesforce Solutions.
Atul Gupta, a dynamic leader, directs CloudVandana’s Implementation Team, Analytics, and IT functions, ensuring seamless operations and innovative solutions.