Enforcing Data Integrity with Salesforce Validation Rules
Validation rules are a cornerstone of data quality management in Salesforce. They allow administrators and developers to prevent incorrect data from being saved to records without requiring Apex code. This article provides practical examples for Sales Cloud (now Agentforce Sales) and Service Cloud (now Agentforce Service) to illustrate effective validation rule implementation.
Sales Cloud (Agentforce Sales) Validation Rules
Sales representatives often interact with data entry, and validation rules can guide them to input accurate information, improving overall data quality. Focus validation efforts on data sales reps can realistically capture or derive from customer conversations. Avoid enforcing fields that require external lookups or data enrichment.
Information Required by Account Type
This example enforces different data requirements based on the Account Type picklist. For Customer account types, specific billing address fields are mandatory.
AND(
OR(ISBLANK(BillingStreet), ISBLANK(BillingCountry), ISBLANK(BillingCity), ISBLANK(BillingState)),
(Text(Type) = "Customer")
)
For variations based on Account Record Type, substitute (Text(Type) = "Customer") with (RecordTypeId = '[INSERT RECORD TYPE ID]').
Country Validation Rules
Standardizing country names prevents data inconsistency. While not strictly a validation rule formula, consider these approaches:
- Salesforce Custom Address Field: Provides address suggestions as users type.
- AddressTools (AppExchange): An application designed for address validation and standardization.
Required Fields for Lead Conversion
When a Lead is converted, ensure essential contact and qualification information is captured.
IsConverted && OR(
ISBLANK(Phone),
ISBLANK(Email),
ISPICKVAL(Rating, "")
)
Required Fields for Closed Won Opportunities
Similar to lead conversion, enforce required fields when an Opportunity is marked as Closed Won.
IsWon && OR(
ISBLANK(Phone),
ISBLANK(Email),
ISPICKVAL(Rating, "")
)
Accounts Created Only Through Lead Conversion
This rule prevents direct Account creation, ensuring all new accounts originate from a qualified Lead.
Step 1: Create a Lead Formula Field
Create a formula field on the Lead object that captures the Lead Created Date. Map this field to a corresponding date field on the Account object (e.g., LeadCreatedDate__c).
Step 2: Create the Account Validation Rule
ISBLANK(LeadCreatedDate__c)
Limit to One Open Opportunity at a Time
Ensure that an Account has only one active Opportunity before a new one can be created.
Step 1: Create an Account Roll-Up Summary Field
Create a roll-up summary field on the Account that counts open Opportunities (where IsClosed is false).
Step 2: Create the Opportunity Validation Rule
Account.NumberofOpenOpportunities__c > 1
Won Opportunities Must Have an Amount Greater Than 0
Prevent Closed Won Opportunities from having a zero or blank Amount for accurate reporting.
OR( ISBLANK(Amount), Amount <= 0 )
Contract End Date Must Be After the Contract Start Date
Enforce a logical date sequence for contract durations.
StartDate > EndDate
Order Products Cannot Be Modified for “Activated” Orders
Prevent modifications to Order Products once an Order has been activated to maintain transactional integrity.
ISPICKVAL(Order.Status, "Activated")
Service Cloud (Agentforce Service) Validation Rules
Service agents need to capture specific information to maintain an audit trail and provide feedback to other departments. Early validation can streamline case management.
Cases Cannot Be Edited When Owned by a Queue
This rule prevents edits to Cases while they are still assigned to a Queue, encouraging agents to take ownership.
LEFT(OwnerId, 3) <> "005"
Note: 005 is the object prefix for User records. This formula checks if the OwnerId starts with 005; if not, it implies ownership by a Queue. This rule can also be applied to Lead Queues.
Case Origin Cannot Be Edited
Prevent changes to the Case Origin field after the Case has been created.
ISCHANGED(Origin) && NOT(ISNEW())
If Case Status Is Not ‘New’, the Contact Cannot Be Blank
Require that a Contact is linked to a Case if its Status is not New.
NOT(ISPICKVAL(Status, 'New')) && ISBLANK(ContactId)
Required Fields to Close a Case
Mandate specific fields (e.g., Reason, Type, Subject, Description) when a Case is closed.
IsClosed && OR(
ISPICKVAL(Reason, ""),
ISPICKVAL(Type, ""),
ISBLANK(Subject),
ISBLANK(Description)
)
To enforce requirements at different stages, use the Status field instead of IsClosed.
Closed Cases Cannot be Modified
Prevent any modifications to a Case once it has been closed.
IsClosed
Tip: For reopened issues, create a new Case and link it using the Parent Case field.
Key Takeaways
- Validation rules are critical for maintaining data integrity in Salesforce without Apex.
- Tailor validation rules to specific object types and business processes within Sales and Service Cloud.
- Focus on enforcing data that users can realistically provide or derive.
- Consider edge cases and potential bypass scenarios (e.g., for administrators or integrations) when designing validation rules.
Leave a Comment