Sub topic 14
Custom permissions: gating features in Apex and flows
This chapter assumes you know how profiles, permission sets, and permission set groups grant access, and that you can read basic Apex. Standard permissions cover Salesforce's built-in features. A custom permission lets you define your own named permission and check it in code or declarative tools.
What a custom permission is
A custom permission is a named flag you create in Setup. It has a label, an API name, and a description. It does nothing by itself. It becomes useful when you grant it to users and check it in Apex, a flow, a validation rule, or a Lightning page visibility filter.
Use one when a feature needs a yes/no switch that does not map to object or field access. Examples are approving discounts, exporting a report to a partner, or seeing an admin-only button.
Creating and granting one
- In Setup, search for Custom Permissions and choose New.
- Enter a label such as
Approve Discountsand an API name such asApprove_Discounts. - Save the permission.
- Open a permission set, go to Custom Permissions, and add
Approve_Discountsto the enabled list. You can also enable it on a profile.
Because the permission lives in a permission set, it travels through permission set groups like any other permission. A user holds it if any assigned profile, permission set, or group grants it. A muting permission set can remove it from a group.
Checking it in Apex
FeatureManagement.checkPermission returns true when the running user has the named permission through any source.
if (FeatureManagement.checkPermission('Approve_Discounts')) {
// Show or run the discount approval logic
} else {
// Hide the feature or return an error
}Pass the API name, not the label. If the name does not exist, the method returns false and does not throw an error. A typo therefore silently denies everyone.
Checking it in a flow and in formulas
In a flow, reference the global variable $Permission.Approve_Discounts in a Decision element or a formula. It evaluates to true or false for the running user. Validation rules, formula fields, and component visibility filters use the same $Permission syntax.
Common errors
- Wrong name. Use the API name. A misspelling returns false, so the feature looks broken for everyone.
- Treating it as security for data. A custom permission only gates the logic that checks it. It does not stop a user from reaching records or fields through other paths. Keep object permissions, field-level security, and sharing in place.
- Forgetting system context. Code that runs without sharing or in a scheduled job still evaluates the permission for the running user, which may be an automated process user. Test with that user.
- Granting through a profile. This works, but it makes the access harder to reuse. Prefer permission sets so the permission can join a group.
Exercise
A flow decision uses $Permission.Approve_Discount but the custom permission's API name is Approve_Discounts. Users who hold the permission still fail the check. Why, and how do you fix it?
Show answer
The name in the flow is misspelled, so the reference does not match the permission and evaluates to false for everyone. Correct the reference to $Permission.Approve_Discounts. Then retest with a user who holds the permission through a permission set.