Sub topic 12
Bypassing rules with custom permissions
This chapter assumes you can write an error condition formula (true means invalid), you know the Setup and Object Manager screens, and you understand permission sets. The last section uses basic Apex. It builds on the earlier chapters on scoping rules by record type, profile, or user.
Why rules need a bypass
A validation rule applies to every save unless the formula says otherwise. That is usually what you want. Some saves legitimately break the rule:
- A data migration loads historical Opportunities that never had an Amount.
- An integration user syncs records from another system in an order that briefly leaves required data out.
- An administrator needs to repair bad records, and the rule blocks the repair.
You could turn the rule off, but that opens a gap for every user. You could hardcode a username or profile name in the formula, but that breaks when the person leaves or the profile is renamed. A custom permission gives you a switch you can grant to specific people and revoke at any time, without editing the rule.
What a custom permission is
A custom permission is a named flag. It has no behavior of its own. It is true for a user if any permission set or profile that user holds includes it. A formula can read the flag through the $Permission global variable, written as $Permission.API_Name.
Setting up a bypass has three parts:
- Create the custom permission in Setup (Custom Permissions). Give it a clear label such as "Bypass Validation Rules". The API name becomes
Bypass_Validation_Rules. - Add the custom permission to a permission set, for example "Data Load Bypass". Assign that permission set to the users who need it.
- Reference
$Permission.Bypass_Validation_Rulesin each validation rule that should honor the bypass.
Adding the bypass clause
Start from a rule that blocks the save when an Opportunity in Negotiation/Review has no positive Amount. Add a NOT($Permission...) clause as the first condition:
AND(
NOT($Permission.Bypass_Validation_Rules),
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)AND returns true only when every argument is true. For a user who holds the permission, NOT($Permission.Bypass_Validation_Rules) is false, so the whole formula is false and the save goes through. For everyone else, that clause is true and the remaining conditions decide the result.
Putting the bypass clause first is a convention, not a requirement. It makes the escape hatch easy to spot when someone reads the formula.
Reuse one permission, or use several
A single org-wide bypass is simple, but it is also broad. Whoever holds it skips every rule that references it. Two other designs are common:
| Design | Example API names | Strength | Weakness |
|---|---|---|---|
| One global bypass | `Bypass_Validation_Rules` | Easy to manage and to document | A user who needs one exemption gets all of them |
| One bypass per object or rule group | `Bypass_Opportunity_Rules`, `Bypass_Account_Rules` | Grants only the exemption needed | More permissions to create, assign, and audit |
| Bypass tied to a business exception | `Can_Edit_Opportunity_Amount` | Names the capability, which reads well in the formula | Only works for rules written around that capability |
For most orgs, a global bypass for integration and data-load users plus capability-style permissions for specific business exceptions is a reasonable split. Do not reuse a capability permission as a general bypass. A permission named Can_Edit_Opportunity_Amount should mean only that.
Capability permissions are not the same as bypasses
A bypass skips a rule. A capability permission is part of the rule's logic. This rule from earlier in the series blocks edits to Amount on existing records unless the user holds the capability:
AND(
NOT(ISNEW()),
ISCHANGED(Amount),
NOT($Permission.Can_Edit_Opportunity_Amount)
)Both patterns use the same mechanism. The difference is intent. The capability permission describes who may do a specific thing. The bypass describes who is exempt from checks in general.
Gotchas
- Typos fail at save time. If the API name in the formula does not match an existing custom permission, Salesforce rejects the rule when you save it. Create the permission first.
- Granting is not automatic. Creating the custom permission does nothing until a permission set or profile includes it and the user is assigned that set. Check with the user's record in Setup, or ask the user to test.
- Validation rules still run during data loads. Data Loader, Bulk API, and integrations all trigger validation rules. A bypass is how you exempt them. The load must run as a user who holds the permission.
- The bypass is evaluated for the running user. If a flow or Apex class runs in system context and saves records,
$Permissionstill reflects the user who started the transaction, not a special system user. - Permissions deploy as metadata. When you move a rule that references a custom permission to another org, deploy the custom permission first or in the same package. Otherwise the rule fails to deploy. Permission set assignments are not part of metadata, so you must assign users again in each org.
- Auditing matters. A bypass lets people write data that the business rules would reject. Limit the permission set to the few users who need it, and review its assignments on a schedule.
Other bypass techniques
Teams sometimes use these instead of custom permissions:
- A hierarchy custom setting with a checkbox. It can be set per user or per profile, and Apex can read it too. It is more flexible, but the rule must reference
$Setup.Settings__c.Bypass__c, and the setting needs data entered in each org, so deployment is harder. - A checkbox field on the record. It lets a loader mark a row as exempt. Anyone with edit access to the field can bypass the rule, so the field needs strict field-level security.
- Hardcoded user or profile checks, such as
$User.Username = "...". Avoid these. They are brittle and invisible to administrators.
Custom permissions are usually the best default. They are metadata, they work with permission sets, and Apex can check them with FeatureManagement.checkPermission('Bypass_Validation_Rules').
Testing a bypass
A bypass is worth a test of its own. The test below creates a user, grants the custom permission through a new permission set, and confirms that an Opportunity the rule would normally reject now saves.
The test assumes the Opportunity rule and the Bypass_Validation_Rules permission already exist in the org. Setup objects such as users and permission sets are inserted inside System.runAs to avoid a mixed DML error, and the Opportunity is inserted in a separate runAs block as the test user.
@IsTest
private class BypassValidationTest {
@IsTest
static void userWithBypassPermissionCanSave() {
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
User u = new User(
Alias = 'bypass',
Email = '[email protected]',
EmailEncodingKey = 'UTF-8',
LastName = 'Bypass',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US',
ProfileId = p.Id,
TimeZoneSidKey = 'America/New_York',
UserName = 'bypass.user' + System.currentTimeMillis() + '@example.com'
);
PermissionSet ps = new PermissionSet(
Name = 'Bypass_Test_Set',
Label = 'Bypass Test Set'
);
System.runAs(new User(Id = UserInfo.getUserId())) {
insert u;
insert ps;
CustomPermission cp = [
SELECT Id FROM CustomPermission
WHERE DeveloperName = 'Bypass_Validation_Rules'
LIMIT 1
];
insert new SetupEntityAccess(ParentId = ps.Id, SetupEntityId = cp.Id);
insert new PermissionSetAssignment(AssigneeId = u.Id, PermissionSetId = ps.Id);
}
System.runAs(u) {
Opportunity opp = new Opportunity(
Name = 'Bypass Opp',
StageName = 'Negotiation/Review',
CloseDate = Date.today().addDays(30)
);
Database.SaveResult result = Database.insert(opp, false);
System.assert(result.isSuccess(), 'User with bypass should be able to save');
}
}
}Also write the opposite test: the same insert as a user without the permission must fail. Without that test, a bypass that applies to everyone would go unnoticed.
Summary of the working model
- A custom permission is a flag, and
$Permission.API_Namereads it for the running user. - A bypass clause of the form
NOT($Permission.X)insideAND(...)makes the rule evaluate to false for holders of the permission. - Assignment happens through permission sets, so access to the bypass is controlled outside the rule.
- Keep bypass permissions narrow, audit their holders, and deploy them before the rules that use them.
Exercise
A rule on Account is AND(ISPICKVAL(Type, "Customer"), ISBLANK(Industry)). You want users with the custom permission Bypass_Account_Rules to be exempt. Write the updated formula, and name the two things you must do outside the formula for a given user to be exempt.
Show answer
The updated formula is AND(NOT($Permission.Bypass_Account_Rules), ISPICKVAL(Type, "Customer"), ISBLANK(Industry)). Outside the formula you must first create the custom permission Bypass_Account_Rules (the rule cannot be saved without it), and then include it in a permission set that you assign to the user. Without the assignment, $Permission.Bypass_Account_Rules is false for that user and the rule still blocks the save.
Exercise
A data load runs as an integration user and fails with your validation rule's error, even though you added NOT($Permission.Bypass_Validation_Rules) to the formula. List two likely causes.
Show answer
The integration user may not be assigned a permission set that includes the custom permission, or the permission set may include a different custom permission with a similar name. The load may also be running as a different user than expected, so check which user the integration authenticates as. Validation rules apply to Bulk API and Data Loader saves, so the bypass must be held by that running user.