Skip to content
Accueil » Salesforce Custom Permissions

Salesforce Custom Permissions

  • yenoussa 

Hey folks, we’re going talk about Salesforce Custom Permissions today!

Imagine you’ve a requirement where you want to give the permission to some users to cancel an Order in Salesforce.

On top of that, the business wants to restrict the right to cancel orders only to those who can approve Orders. That’s were custom permissions come ! Let’s deep dive into this feature…

What are custom permissions in Salesforce?

Custom permissions are permissions you need to create to differenciate users that can perform an action from those who can’t.

For Salesforce features that come out of the box, of course you can control the permissions by editing the profile or permission set, but when it comes to the business rules expected by you company, you have to go for custom !

Why not just checking user’s Permission Set / Profile?

Yes you’re right. It’s possible and I’ve seen it on several Salesforce implementation. But…

  • It’s not possible to check if a user has a given Permission Set in a validation Rule: you need to go with a Trigger, bulkify, write the Unit test… arf… lot of work 😉
  • If you want to check a long list of user profile in a validation rule, it will become hard to read…

Long story short: don’t do that. It’s painful and you implementation is not sustainable. Prefer Custom permissions.

Create the custom permissions

Create 2 salesforce custom permissions

For this requirement, we’ll need 2 custom permissions:

  • Approve order
  • Cancel order

To implement the fact that Cancel Order requires Approve Order, check the required custom permissions section for Cancel order and add Approve order.

Add a custom permission as required for this custom permission

Assign Salesforce Custom Permissions to users

There are 2 ways to assign Custom Permissions to a user:

Permission set method

  • From Setup, enter Permission Sets in the Quick Find box, then select Permission Sets.
  • Select the permission Set you want to add the custom permission to
  • Go to custom permission and add the custom permissions you want
It's not possible to add the custom permission set without adding the other, as expected

Note that the requirement is perfectly implemented: if I try to add the Cancel Order Custom permission set without the Approve Order, it’s not allowed !

Profile method

For adding a Custom Permission Set to a profile, it’s the same process as for permission sets:

  • From Setup, enter Profile in the Quick Find box, then select Permission Sets.
  • Select the profile you want to add the custom permission to
  • Go to “Enabled Custom Permissions” and add the custom permissions you want

Implement the Custom Permission checks

Check Custom permission in validation rule

Easy, to do, just use the Permission in the validation rule:

Check custom permission in a Trigger

For an implementation in a Trigger and more generaly speaking in any Apex code, use the static FeatureManagement.checkPermission method as per below.

(note that to keep the code simple, the below implementation doesn’t respect the trigger best practice, please check them )

Check custom permission in a Salesforce Flow

Same as in the Validation Rule, use the Permission variable to check if the current user has the custom permission or not.

The below {!$Permission.Cancel_Order_CUST_PERM} allow me to do this check in the implemented flow.

Use custom permission in a Salesforce flow

As per the newly Salesforce release Winter’24, it’s now possible to add error to prevent record save in trigger, so don’t hesitate, use it !

Add a custom error message in a Salesforce flow

Leave a Reply

Your email address will not be published. Required fields are marked *