Sub topic 16
Troubleshooting and deployment
This chapter assumes you can write a validation rule formula, know the basic functions (AND, OR, ISBLANK, ISPICKVAL, ISCHANGED), and have used Setup, the Object Manager, and debug logs. It covers two problems. The first is a rule that does not behave as expected. The second is a rule that works in a sandbox and causes trouble when you move it to another org.
Start with the symptom
Most problems fall into two groups: the rule never fires, or it fires when it should not. Sort the symptom before you edit the formula.
| Symptom | Likely causes | First check |
|---|---|---|
| Rule never fires | Rule is inactive, the formula is false for that data, a record type or profile clause excludes the user, or the user holds a bypass permission | Active flag, then the `$Permission` and `RecordType` clauses |
| Rule blocks valid saves | A blank value is treated as zero or empty text, the picklist is compared incorrectly, or the parent record is blank in a cross-object reference | `ISBLANK` guards and the `ISPICKVAL` syntax |
| Rule blocks unrelated edits | Old records already violate the rule, and any edit re-evaluates it | Add `ISCHANGED` or `ISNEW` |
| Error appears at the top of the page, not on the field | The error location field is not on the page layout | Page layout and field-level security |
| Data load or integration fails | The rule fires for API users | Bypass permission or a load plan |
| Apex tests fail after deployment | Test data now violates the new rule | Test data factories |
flowchart TDThe block above is not a diagram. Use the one below.
Read the debug log
The debug log shows what Salesforce evaluated. Set the log level for the Validation category to Info or higher, then save a record as the affected user. The log contains these events:
VALIDATION_RULEnames the rule being evaluated.VALIDATION_FORMULAshows the formula and the field values it used.VALIDATION_PASSmeans the formula returned false, so the save continues.VALIDATION_FAILmeans the formula returned true, so the save is blocked.
The VALIDATION_FORMULA line is the most useful. It often shows that a field you assumed was filled in was empty, or that a before-save flow or trigger changed a value before the rule ran.
Order of execution surprises
Custom validation rules run after before-save record-triggered flows and before triggers. They see the values those tools set, not only what the user typed. Two consequences follow:
- A flow that fills in
Amountcan make a rule pass even though the user left the field blank. - A before trigger that changes
StageNamecan make a rule fire on a value the user never entered.
Rules run before after-save logic, so an after-save flow or trigger cannot rescue a record the rule has already blocked.
Isolate a faulty formula
When the log does not make the cause obvious, test the formula in pieces:
- Create a temporary Formula (Checkbox) field on the object. Paste one clause from the rule into it.
- Add the field to a list view or report and compare it with the records that misbehave.
- Add clauses back one at a time until the result changes.
- Delete the temporary field when you are done.
Common formula errors are worth recognizing on sight:
Picklist fields are only supported in certain functionsmeans you compared a picklist directly. UseISPICKVAL(StageName, "Closed Won"), orTEXT(StageName)inside a comparison.Syntax error. Missing ')'usually points at an unbalanced bracket in a longANDorOR. Indent each clause on its own line.Compiled formula is too big to executemeans the formula exceeds the compiled size limit. Move repeated cross-object lookups into formula fields on the object and reference those fields. Check the current Salesforce documentation for the exact limits.
Fix rules that block unrelated edits
A validation rule is not retroactive. It checks a record only when someone saves it. Records that already violate the rule stay in the database until a user edits them, and then the edit fails with an error about a field the user did not touch.
You have three options:
- Clean the data first with a report and a data load.
- Restrict the rule to changes with
ISNEW()orISCHANGED(field). - Accept the failure and tell users to correct the field when they see the message.
Deployment basics
Validation rules are metadata. In Salesforce DX source format, each rule is a file under its object, for example force-app/main/default/objects/Opportunity/validationRules/Amount_Required_In_Negotiation.validationRule-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ValidationRule xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Amount_Required_In_Negotiation</fullName>
<active>true</active>
<errorConditionFormula>AND(
NOT($Permission.Bypass_Validation_Rules),
ISPICKVAL(StageName, "Negotiation/Review"),
OR(ISBLANK(Amount), Amount <= 0)
)</errorConditionFormula>
<errorDisplayField>Amount</errorDisplayField>
<errorMessage>Enter an Amount greater than zero before moving to Negotiation/Review.</errorMessage>
</ValidationRule>In XML, < must be written as <. Salesforce CLI does this for you when it retrieves the rule.
The active element travels with the rule. If you deploy true, the rule starts blocking saves in the target org as soon as the deployment finishes.
Dependencies
A rule fails to deploy if anything it references is missing in the target org. Deploy these first, or in the same package:
- Custom fields used in the formula.
- The custom permission behind
$Permission.Bypass_Validation_Rules. - Record types and the parent object fields used in cross-object references.
Permissions are data, not metadata
You can deploy a custom permission and the permission set that contains it. You cannot deploy the assignment of that permission set to users. After the first deployment, assign the bypass permission set in the target org, or your integration user will be blocked.
Deploy inactive, then activate
For a high-risk rule, deploy it with <active>false</active>. Run your checks in the target org, then activate it in Setup or with a second deployment. This keeps a bad formula from blocking users while you verify it.
Deployment and Apex tests
A new rule can break Apex tests that create records. A test that inserts an Opportunity at Negotiation/Review without an Amount now throws a DmlException, although the Apex code did not change.
Fix the test data, not the rule:
@IsTest
public class TestDataFactory {
public static Opportunity buildNegotiationOpportunity() {
return new Opportunity(
Name = 'Factory Opp',
StageName = 'Negotiation/Review',
CloseDate = Date.today().addDays(30),
Amount = 1000
);
}
}Keep test data in one shared factory so a new rule needs one fix, not many. Tests that check the rule itself should still use Database.insert(record, false), as in the testing chapter.
Integration and data load checks
Validation rules also run for API calls, Data Loader, and other integrations. Before activating a rule in production, check three things:
- Does the integration user hold the bypass permission, if it should?
- Will a bulk load that contains bad rows fail, or fail partially? With
allOrNonefalse, good rows save and bad rows return errors in the result. - Does anything depend on the error text? Integrations that parse
errorMessagebreak when you reword it.
Exercise
After a deployment, users report that editing the Phone field on old Accounts fails with an error about Billing Postal Code. The new rule uses REGEX on BillingPostalCode and has no ISCHANGED clause. Why does this happen, and what is one fix?
Show answer
The rule is evaluated on every save, so any edit to an Account re-checks the postal code, and old records with bad codes now fail. One fix is to add ISCHANGED(BillingPostalCode) to the AND so the check runs only when the code is edited. Another fix is to clean the existing postal codes first.
Exercise
A validation rule passes in the sandbox but your integration user is blocked in production, although the formula contains NOT($Permission.BypassValidationRules). What did the deployment most likely leave out?
Show answer
The permission set assignment. The custom permission and permission set deployed as metadata, but assigning the permission set to the integration user is data and must be done in production.