Adminstrator · Apex · Salesforce

Use Salesforce Custom Permission. Don’t use Profile/User id to skip validation rule.

Use salesforce custom permission to user to access custom process or apps. Many use case – validation rule should not executed for admin profile or specific user.

Right now admin/developer hard coding profile/user ID or Name in the validation rule to skip. See below

  1. Skipping based on Profile Name
AND ( 
  ISCHANGED( OwnerId ), 
   $Profile.Name != 'Admin' 
)

2. Skipping based on Profile Id

AND ( 
    ISCHANGED( OwnerId ), 
    $User.ProfileId != 'ooe12345678990' 
)

Now salesforce introduced “Custom Permission” to avoid hard code Name and ID in validation rule and etc.

Create new custom permission:

  1. From Setup, enter Custom Permissions in the Quick Find box, then select Custom Permissions.
  2. Click New.
  3. Enter the permission information:
    • Label—the permission label that appears in permission sets
    • Name—the unique name that’s used by the API and managed packages
    • Description—optionally, a description that explains what functions the permission grants access to, such as “Approve time-off requests.”
    • Connected App—optionally, the connected app that’s associated with this permission
  4. Click Save.

Assign the custom permission to Profile or Permission Set. Salesforce strongly recommend to use Permission Set and assign user.

Test Case:

  1. Created custom permission called “NoClosedWonWithoutOPPLI”.
  2. Created Permission set called “NoClosedWonWithoutOPPLI” (using same name as custom permission) and Added custom permission.
  3. Assigned my user to Permission Set.
  4. Now all set. Please see below validation rule

After Customer Permission:

AND( Not($Permission.NoClosedWonWitoutOPPLI), 
Text(StageName) = "Closed Won",
NOT(HasOpportunityLineItem) 
)

In the feature you want to skip this validation rule for more user you can assign permission to that user. That’s it no need to change the validation rule and deployment.

This can be used in Workflow Rule, Process Builder, Flow, Apex Code, Visualforce page.

Reference Resource:

Custom Permission

Demo Video

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s