Sub topic 16
Troubleshooting and deployment
This chapter assumes you can write a validation rule formula, know how ISBLANK and ISPICKVAL behave, and have used Setup and a sandbox. It covers how to find out why a rule misbehaves and how to move rules between orgs safely.
Rules that never fire
Check these in order:
- The rule's Active box is selected.
- The formula is an error condition. It must return true for invalid data. A rule written as "what is valid" blocks every good record instead.
- Blank values are handled. A comparison such as
Amount <= 0on a blank field behaves differently fromISBLANK(Amount), so test both. - Record scope clauses, such as record type or profile checks, do not exclude the record you are testing.
Rules that fire unexpectedly
- Old bad data. A rule runs on every save, so a record that was invalid before the rule existed blocks unrelated edits. Fix the data or add
ISCHANGEDto the condition. - Automation saves. A flow or Apex trigger that updates a record runs the rule too. The user sees an error for a change they did not make.
- Data loads and integrations. Bulk loads hit rules row by row. Use the bypass permission from the earlier chapter.
- Field-level security. The formula reads values even when the user cannot see the field, so the error can mention a field the user cannot open.
Finding the cause with debug logs
Open a debug log for the affected user and set the Workflow category to FINER. The log shows VALIDATION_RULE, VALIDATION_FORMULA, and VALIDATION_PASS or VALIDATION_FAIL lines. The formula line lists the field values the rule actually saw, which settles most "why did it fire" questions.
Formula compile errors
- Picklists need
ISPICKVALorTEXT. Comparing one with=fails. - Text and number types cannot be mixed in one comparison.
- A rule formula is limited to 5,000 characters. Split long logic into several rules or move it to a flow.
Deploying rules
A validation rule is metadata. Each one is a file in your project, and the active setting travels with it.
<?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(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>Save this as force-app/main/default/objects/Opportunity/validationRules/Amount_Required_In_Negotiation.validationRule-meta.xml. Deploy it with:
sf project deploy start --source-dir force-app/main/default/objects/Opportunity/validationRules --test-level RunLocalTestsKeep these points in mind:
- Dependencies go first. Custom fields and custom permissions that the formula uses must already exist in the target org or be in the same deployment.
- Existing Apex tests can fail. A new rule can reject test data that older tests insert. Run all local tests in a sandbox before production.
- Deploy inactive when needed. Set
activeto false, deploy, load or clean data, then activate. - Change sets and packages carry the same metadata, but you must add each rule manually to a change set.
Exercise
After deployment, a nightly integration begins failing with FIELDCUSTOMVALIDATION_EXCEPTION, but users saving the same records manually succeed. Name two likely causes and one way to confirm.
Show answer
The integration user may lack the bypass custom permission, or it may send blank or different values than users enter, such as no Amount at the Negotiation/Review stage. Confirm by running the integration as that user with a debug log and reading the VALIDATIONFORMULA and VALIDATIONFAIL lines.