+ Start a Discussion
Trainee second visionitTrainee second visionit 

how to restrict one user to delete record..

please help me...
i have an one profile xyz that have an multiple users.xyz profile has full access permission.
my question is to restrict only one user to delete permission on that profile.
can i use trigger?then reply me code pls....

thanks.
Best Answer chosen by Trainee second visionit
Avishek Nanda 14Avishek Nanda 14
Hi ,

I test the same code and working fine. 

Create a Custom Label Called  UserGroup and add userid (18 Digit) in comma separated value if you have multiple. 
 
Handler Class

public class CannotDeleteAccount {
    public static void NoDeleteAccount (List<Account> accounts){
        system.debug('before delete trigger');
        String Userid = UserInfo.getUserId();
        System.debug('Current User id'+Userid);
        set<string> strkey = new set<string>(Label.UserGroup.split(','));
        System.debug('strkey'+strkey);
        
        for (Account acc: accounts){
            If(strkey.contains(Userid)){
                acc.addError('You cannot delete the Account');
            }
        }
    }
}
 
Call the Method From trigger 

Trigger accountTrigger on Account (before delete) {
    if(Trigger.isBefore){
        System.debug('Inside the Trigger');
        CannotDeleteAccount.NoDeleteAccount(Trigger.Old);
    }
}

This should work without any issue. Simply Copy-Paste the above code will work. 

All Answers

Vinod ChoudharyVinod Choudhary
Hi,

Setup|AdministrationSetup|Security Controls|Sharing Settings.

OR

Settings >> Manage Users >> Profiles >> Object Permissions >> Uncheck on "Delete" next to the appropriate records.

Hope this helps you.
mukesh guptamukesh gupta
Hi ,

For this situation you need to create a permission set for particular user that want to assign permission. Please follow this url 

https://developer.salesforce.com/docs/atlas.en-us.securityImplGuide.meta/securityImplGuide/perm_sets_overview.htm

if you need any assistance please let me know 

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh




 
Vinod ChoudharyVinod Choudhary
You can writte trigger also for same.
Example:
trigger preventDelete on Contract (before delete) {
    for(someSObject record: Trigger.old) {
        if(record.Status != 'something') {
            record.addError('You may not delete a contract.');
        }
    }
}


 
Trainee second visionitTrainee second visionit
@vinod choudhary hows this possilbe for one specific user...
 
Trainee second visionitTrainee second visionit
hii @ mukesh gupta,
can you restrict any profile permission of single user through permission set?
Avishek Nanda 14Avishek Nanda 14
​Hi,  You can't use a Validation Rule to prevent a Deletion.  That has to be a trigger. In You question you didn't mention any object Type. Assuming You want to restrict user deleting account record. Please Find the Below Code. You can change the Code according to your need.  Bellow code I have used Custom Label. UserGroup. In Future, if your org needs to extend this feature to any other user then You can add the user id to the Label without even changing the code. And also add this  Check Recursive you have to add the class as well. This is a best practice we use to make sure the trigger doesn't go into recursive. 
 
Add this Recursive Class. 

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
        if(run){
            run=false;
            return true;
        }else{
            return run;
        }
    }
}

You Handler Class 

public class CannotDeleteAccount {
    public static void NoDeleteAccount (List<Account> accounts){
        system.debug('before delete trigger');
        String Userid = UserInfo.getUserId();
        set<string> strkey = new set<string>(Label.UserGroup.split(','));
        
        for (Account acc: accounts){
            If(strkey.contains(Userid)){
                a.addError('You cannot delete the Account');
            }
        }
    }
}

Trigger :

trigger accountTrigger on Account (before delete) {
    if(checkRecursive.runOnce()){
        if(Trigger.isBefore){
            System.debug('Inside the Trigger');
            CannotDeleteAccount.NoDeleteAccount(Trigger.Old);
        }
    }
}

 
mukesh guptamukesh gupta
Hi ,

For example, let’s say you have several users with a profile called Sales User. This profile allows assignees to read, create, and edit leads. Some, but not all, of these users also need to delete and transfer leads. Instead of creating another profile, create a permission set.

Regards
Mukesh


 
Vinod ChoudharyVinod Choudhary
You can create a permission set for the use which you want to restrict. and us this for that particular user.

Or

In the trigger, you can put a condition for that particular user ( But it will going to be static and not the best way).

Thanks
Vinod
 
Trainee second visionitTrainee second visionit
yes..@mukesh gupta you are absolutely right but my question is 

There are multiple users, who have same profile and have permission of delete records. 
     We have to restric only one user from deleting records in any object.
 
Trainee second visionitTrainee second visionit
@Vinod Choudhary.. correct but in my situation i think trigger is best solution. so please help me to write a trigger..
 
Trainee second visionitTrainee second visionit
@Avishek Nanda 14 your code is not running properly.i want to restrict deletion of account record to a specific user like name='PQR' or userID='0057F000000ibdv' then how to  achive this?
Avishek Nanda 14Avishek Nanda 14
Hi ,

I test the same code and working fine. 

Create a Custom Label Called  UserGroup and add userid (18 Digit) in comma separated value if you have multiple. 
 
Handler Class

public class CannotDeleteAccount {
    public static void NoDeleteAccount (List<Account> accounts){
        system.debug('before delete trigger');
        String Userid = UserInfo.getUserId();
        System.debug('Current User id'+Userid);
        set<string> strkey = new set<string>(Label.UserGroup.split(','));
        System.debug('strkey'+strkey);
        
        for (Account acc: accounts){
            If(strkey.contains(Userid)){
                acc.addError('You cannot delete the Account');
            }
        }
    }
}
 
Call the Method From trigger 

Trigger accountTrigger on Account (before delete) {
    if(Trigger.isBefore){
        System.debug('Inside the Trigger');
        CannotDeleteAccount.NoDeleteAccount(Trigger.Old);
    }
}

This should work without any issue. Simply Copy-Paste the above code will work. 
This was selected as the best answer
mukesh guptamukesh gupta
Hi ,

Please apply this code
trigger RestrictDeleteAccount on Account (before delete) {
    List<User> a = [select id from user where id = '0057F000000ibdv' LIMIT 1]; 
	if(a.size() > 0 ){
         for(Account acc :Trigger.old){
          acc.adderror('Account Cannot be deleted');
         
	 }
    }
}

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh



 
Trainee second visionitTrainee second visionit
@mukesh gupta how its possible? your code not getting current users info then....
Trainee second visionitTrainee second visionit
Thank you so much @Avishek Nanda 14...