mirror of
https://github.com/forcedotcom/afv-library.git
synced 2026-08-06 07:49:37 +08:00
* @W-22252932 [Meta Skill] Skill Creator * @W-22252932 [Meta Skill] Skill Creator * @W-22252932 [Meta Skill] Skill Creator * @W-22252932 [Meta Skill] Skill Creator * @W-22252932 [Meta Skill] Skill Creator * @W-22252932 [Meta Skill] Skill Creator * @W-22252932 [Meta Skill] Skill Creator * @W-22252932 [Meta Skill] Skill Creator
4.0 KiB
4.0 KiB
| name | description | metadata | ||||||
|---|---|---|---|---|---|---|---|---|
| generating-validation-rule | Create and update Salesforce validation rules for any standard or custom object. Use when the user asks to add a validation rule, enforce field requirements, prevent invalid data entry, or implement record-level business logic constraints. TRIGGER when: user says validation rule, field validation, required field check, data constraint, record validation, prevent invalid data. DO NOT TRIGGER when: user asks about Flow validation or Apex validation logic — delegate to generating-flow or generating-apex. |
|
Generating Validation Rules
Create validation rules that enforce business logic at the record level.
Scope
- In scope: Creating new validation rules, updating existing rules, formula syntax, error message placement, activation/deactivation.
- Out of scope: Flow-based validation (use
generating-flow), Apex triggers for complex validation (usegenerating-apex).
Required Inputs
- Target object (standard or custom)
- Business rule to enforce (in plain language)
- Error message text
- Error location (top of page or specific field)
Defaults:
- Active: true
- Description: auto-generated from the business rule
Workflow
- Identify the object — confirm the API name of the target object.
- Understand the rule — translate the business requirement into a boolean formula.
- Check existing rules — look for overlapping validation rules on the same object.
- Read the metadata template — load
./assets/validation_rule_template.xmlbefore generating. - Write the formula — the formula must evaluate to
TRUEwhen the data is invalid. For common formula patterns, read./references/formula_patterns.md. - Set error message — clear, user-friendly message explaining what's wrong and how to fix it.
- Create metadata file — generate the
.validationRule-meta.xmlusing the template structure. Compare against./examples/require_email_on_contact.xmlfor expected output format. - Syntax check — verify the formula compiles (field API names, function syntax).
- Edge case review — test with null values, blank strings, boundary conditions.
Rules
| Constraint | Rationale |
|---|---|
| Formula evaluates to TRUE on invalid data | Salesforce convention — TRUE = error |
| Use API names, not labels | Labels change; API names are stable |
Handle null fields with ISBLANK() |
Prevent null pointer errors |
Use PRIORVALUE() only in update context |
Not available on insert |
Gotchas
| Issue | Resolution |
|---|---|
| Rule fires on record types it shouldn't | Add RecordType.DeveloperName check to formula |
| Rule blocks data migration/bulk loads | Add bypass via Custom Permission or Custom Metadata |
ISCHANGED() always false on insert |
Guard with NOT(ISNEW()) before using ISCHANGED() |
| Formula too long (> 5000 chars) | Split into multiple rules or use a helper formula field |
| Error on wrong field | Verify field API name in errorConditionFormula attribute |
Output Expectations
Deliverables per validation rule:
<ObjectName>.validationRule-meta.xml
File structure follows the template in ./assets/validation_rule_template.xml.
Cross-Skill Integration
| Need | Delegate to |
|---|---|
| Complex multi-object validation | generating-apex (trigger-based) |
| Flow-based validation with user prompts | generating-flow |
| Custom object creation | generating-custom-object |
| Field creation for formula references | generating-custom-field |
Reference File Index
| File | When to read |
|---|---|
./assets/validation_rule_template.xml |
Before generating any validation rule metadata file |
./references/formula_patterns.md |
When writing the formula — common patterns and functions |
./examples/require_email_on_contact.xml |
To verify generated output matches expected format |