generating-validation-rule

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

When to Use This Skill

何时使用此技能

Use this skill when you need to:
  • Create validation rules to enforce data quality
  • Prevent invalid records from being saved
  • Generate validation rule metadata with formulas
  • Add business logic validation to objects
  • Troubleshoot deployment errors related to validation rules
当您需要以下操作时,请使用此技能:
  • 创建验证规则以确保数据质量
  • 防止无效记录被保存
  • 生成带公式的验证规则元数据
  • 为对象添加业务逻辑验证
  • 排查与验证规则相关的部署错误

Specification

规范

ValidationRule Metadata Specification

ValidationRule元数据规范

📋 Overview

📋 概述

Validation Rules are declarative metadata components used to enforce data quality and business logic in Salesforce. They evaluate a formula expression when a record is saved and prevent the save operation if the expression returns TRUE.
Validation Rules是Salesforce中用于确保数据质量和执行业务逻辑的声明式元数据组件。当保存记录时,它们会评估一个公式表达式,如果表达式返回TRUE,则会阻止保存操作。

🎯 Purpose

🎯 用途

-Enforce business rules at the data layer -Prevent invalid or incomplete records from being saved -Display meaningful error messages to guide users
  • 在数据层执行业务规则
  • 防止无效或不完整的记录被保存
  • 显示有意义的错误消息以引导用户

⚙️ Required Properties

⚙️ 必填属性

Core Validation Rule Properties

核心验证规则属性

  • fullName
    • The unique API name of the validation rule
    • Must start with a letter
      • Can contain letters, numbers, and underscores
    • Cannot end with an underscore
    • Cannot contain consecutive underscores
    • Cannot exceed 40 character.
  • active -Indicates whether the validation rule is enabled true → Rule is enforced false → Rule is inactive
  • errorConditionFormula
  • The logical formula that evaluates record data
  • Must return TRUE or FALSE
  • If TRUE, the validation rule triggers an error
  • errorMessage
    • The message displayed to the user when validation fails
  • Maximum length: 255 characters
  • fullName
    • 验证规则的唯一API名称
    • 必须以字母开头
      • 可包含字母、数字和下划线
    • 不能以下划线结尾
    • 不能包含连续下划线
    • 长度不能超过40个字符
  • active
    • 指示验证规则是否启用 true → 规则生效 false → 规则处于非激活状态
  • errorConditionFormula
  • 用于评估记录数据的逻辑公式
  • 必须返回TRUE或FALSE
  • 如果返回TRUE,验证规则将触发错误
  • errorMessage
    • 验证失败时显示给用户的消息
  • 最大长度:255个字符

Specific Function Guidelines

特定函数准则

  • TEXT - TEXT() function MUST NOT be used with Text fields, to fix this you can just remove the TEXT() function.
  • CASE - In salesforce CASE() function, last parameter is the default value. Admins often miss to provide this and number of parameters to CASE() function are always even.
  • VALUE - VALUE() function should only be used with Text fields. If a number is being used as a parameter to the VALUE() function, remove the VALUE() function.
  • DAY - DAY() function should only be used with Date fields. If a Datetime field is being used as a parameter to the DAY() function, convert it into a Date first.
  • MONTH - MONTH() function should only be used with Date fields. If a Datetime field is being used as a parameter to the MONTH() function, convert it into a Date first.
  • DATEVALUE - DATEVALUE() function should only be used with DateTime fields. If a Date is being used as a parameter to the DATEVALUE() function, remove the DATEVALUE() function.
  • ISPICKVAL - If checking equality of a picklist type field, the function ISPICKVAL() MUST be used.
  • ISCHANGE - Use ISCHANGE() function to check the value of a record has changed.
  • TEXT - TEXT()函数不得与文本字段一起使用,解决方法是直接移除TEXT()函数。
  • CASE - 在Salesforce的CASE()函数中,最后一个参数是默认值。管理员经常遗漏这个参数,且CASE()函数的参数数量必须为偶数。
  • VALUE - VALUE()函数只能与文本字段一起使用。如果将数字作为VALUE()函数的参数,请移除VALUE()函数。
  • DAY - DAY()函数只能与日期字段一起使用。如果将日期时间字段作为DAY()函数的参数,请先将其转换为日期类型。
  • MONTH - MONTH()函数只能与日期字段一起使用。如果将日期时间字段作为MONTH()函数的参数,请先将其转换为日期类型。
  • DATEVALUE - DATEVALUE()函数只能与日期时间字段一起使用。如果将日期作为DATEVALUE()函数的参数,请移除DATEVALUE()函数。
  • ISPICKVAL - 检查选择列表类型字段的相等性时,必须使用ISPICKVAL()函数。
  • ISCHANGE - 使用ISCHANGE()函数检查记录的值是否已更改。

Critical Rules

关键规则

  1. Formula XML Handling(MOST COMMON ERROR)
    • ANY errorConditionFormula containing XML tags MUST be inside a CDATA section in the metadata XML.
  2. Interpretation of "Update" Instructions. When receiving instructions to modify a formula, distinguish between a replacement and an addition:
    • "Update the formula to [Action]": Completely replace the existing formula logic with the new requirement.
    • "Update the formula to also [Action]": Keep the existing logic and append the new requirement (usually by wrapping the logic in an AND() or OR() function).
  3. File Format Requirement
    • Validation rule files MUST always use the
      .validationRule-meta.xml
      extension.
  1. 公式XML处理(最常见错误)
    • 任何包含XML标签的errorConditionFormula必须放在元数据XML的CDATA部分中。
  2. “更新”指令的解读:当收到修改公式的指令时,区分替换和追加:
    • “将公式更新为[操作]”:用新需求完全替换现有公式逻辑。
    • “将公式更新为同时[操作]”:保留现有逻辑并追加新需求(通常通过将逻辑包裹在AND()或OR()函数中实现)。
  3. 文件格式要求
    • 验证规则文件必须始终使用
      .validationRule-meta.xml
      扩展名。