Sub topic 14
Custom permissions: gating features in Apex and flows
This chapter assumes you can create permission sets in Setup, assign them to users, and read basic Apex and Flow Builder logic. It builds on the permission set and permission set group chapters.
Why custom permissions exist
Standard permissions cover Salesforce's built-in features, such as "Edit" on Account or "Export Reports". Your own logic often needs a switch that has no built-in equivalent. Examples are "can approve discounts above 10%", "can see the beta pricing screen", or "can bypass the duplicate check during data loads".
A custom permission is a named, org-defined switch. It holds no data and grants no access to any object. It records whether the running user has been given that switch. You assign it the same way as any other permission, so it also works with permission set groups, muting, and the usual auditing.
The alternatives are worse. Hardcoding profile names in Apex breaks when a profile is renamed. Hardcoding user IDs or a custom setting of usernames is hard to audit. A checkbox on the User record does not appear in permission set assignment reports. A custom permission uses the access model you already manage.
Create and assign a custom permission
- In Setup, open Custom Permissions and click New.
- Enter a Label (Approve Discounts) and a Name (
Approve_Discounts). The name is the API name that code references. - Optionally add a description, a connected app association, and required custom permissions. A required permission must also be enabled before this one can be enabled.
- Open a permission set, go to Custom Permissions, and add
Approve_Discountsto Enabled Custom Permissions.
Profiles can also enable custom permissions. Prefer a permission set, for the same reasons as everywhere else in this tutorial. A permission set can be bundled into groups and changed without touching the profile.
Check it in Apex
FeatureManagement.checkPermission takes the permission's API name and returns a Boolean for the running user. It returns true if any assigned profile, permission set, or permission set group (after muting) enables it.
public with sharing class DiscountService {
public class DiscountException extends Exception {}
public static void validateDiscount(Decimal percent) {
Boolean needsApproval = percent > 10;
if (needsApproval && !FeatureManagement.checkPermission('Approve_Discounts')) {
throw new DiscountException(
'Discounts above 10% require the Approve Discounts permission.'
);
}
}
}Notes on behavior:
- The check is against the running user, so inside
System.runAsin a test it evaluates therunAsuser. To test both paths, create two users and assign the permission set to only one. - A misspelled name does not raise a compile error, because the name is a string. In practice it returns
false, which looks like "access denied". Keep the name in one constant, and cover both outcomes with tests. - The check reads from the user's access, not from the sharing mode of the class. A
without sharingclass still sees the real running user's custom permissions.
Check it in flows and formulas
Every enabled custom permission is exposed through the $Permission global variable. In Flow Builder, add a Decision element and compare {!$Permission.Approve_Discounts} to {!$GlobalConstant.True}. Use the result to route users to an approval screen or to skip a step.
The same variable works in:
- Validation rules, for example
AND(Discount__c > 10, NOT($Permission.Approve_Discounts)). - Formula fields and formula resources.
- Dynamic Forms and Lightning page component visibility, through a Permissions filter.
- Visualforce pages, through
$Permission.
Lightning web components import the permission as a value:
import hasApproveDiscounts from '@salesforce/customPermission/Approve_Discounts';
export default class DiscountPanel {
get canApprove() {
return hasApproveDiscounts;
}
}Where each check belongs
| Where you check | Good for | Limit |
|---|---|---|
| Apex `checkPermission` | Server-side enforcement in triggers, services, and controllers | Only protects code paths that call it |
| Validation rule | Blocking saves from any entry point, including API and import | Can only inspect the record and `$Permission` |
| Flow decision | Routing and screen logic | Flows running in system context still see the user's permission, but admins can bypass flow by other means |
| LWC import or page filter | Showing or hiding UI | Hiding a button does not stop a direct API call |
Enforce on the server, and use the UI check only for convenience. A hidden button is not security.
Metadata and deployment
A custom permission is a CustomPermission metadata component. The permission sets that enable it list it in a customPermissions element, so deploy both together.
<?xml version="1.0" encoding="UTF-8"?>
<CustomPermission xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Approve Discounts</label>
<description>Allows approving discounts above 10 percent</description>
<isLicensed>false</isLicensed>
</CustomPermission>Inside a permission set file, enable it like this:
<customPermissions>
<enabled>true</enabled>
<name>Approve_Discounts</name>
</customPermissions>If the permission set is deployed to an org where the custom permission does not exist yet, the deployment fails. Include the CustomPermission component in the same package. Tests that depend on the permission set also need the metadata deployed first.
Auditing who holds a custom permission
Custom permissions are stored as setup entities, so SetupEntityAccess links them to the permission sets and profile-owned permission sets that enable them.
SELECT Parent.Label, Parent.IsOwnedByProfile, Parent.Profile.Name
FROM SetupEntityAccess
WHERE SetupEntityId IN (
SELECT Id FROM CustomPermission WHERE DeveloperName = 'Approve_Discounts'
)Rows with IsOwnedByProfile = true mean a profile enables it directly. Join that result with PermissionSetAssignment to list the users. Permission sets that appear only inside a group are included, but the query does not show the group name. Check the group's membership in Setup if you need that.
Common errors
- The user has the permission set but the check returns false. The permission set may be in a permission set group that is not yet Updated, or a muting permission set in that group may mute
Approve_Discounts. Check the group status and its muting set. - A required permission is missing. If you configured a required custom permission, the dependent one cannot be enabled without it.
- Name mismatch. Use the API name (
Approve_Discounts), not the label. In a managed package, follow the package's documentation for namespace handling. - Checking in a test with the wrong user. The test runs as the test-running user unless you use
System.runAs.
Exercise
A flow shows an "Approve" screen only when {!$Permission.Approve_Discounts} is true. A user was added to the permission set that enables it, yet the screen never appears. The permission set is inside a permission set group. Name two things to check, and say why hiding the screen alone would not stop this user from approving through the API.
Show answer
Check that the group's status is Updated (not Updating, Outdated, or Failed) and that a muting permission set in the group does not mute Approve_Discounts. Hiding the screen is only a UI check. An API call or another flow bypasses it unless the same permission is enforced server-side, for example with FeatureManagement.checkPermission in Apex or a validation rule that uses $Permission.