Understanding the Challenge
Teams running Agentforce often face the challenge of validating user inputs against Salesforce validation rules, including required fields, field formats, and cross-field validations. The goal is to surface validation feedback back to the user during the conversation, rather than just handling DML failures.
The root cause of this challenge is that Agentforce conversational flows do not natively enforce Salesforce validation rules. However, by leveraging Apex describe metadata, validation rule evaluation, and savepoints with error parsing, developers can build custom solutions to address this issue.
Technical Solution
To enforce Salesforce validation rules in Agentforce conversational flows, developers can use a combination of Apex and Agentforce features. One approach is to use Topic Instructions and Action Input Instructions to refine conversational user inputs into a set of data acceptable to Salesforce.
Apex Example
// Example Apex code to validate user input against Salesforce validation rules
public class ValidateUserInput {
public static void validateInput(String userInput) {
// Use Apex describe metadata to retrieve validation rules for the object
Map fields = Schema.SObjectType.YourObjectName.fields.getMap();
// Iterate through the fields and validate the user input against each field's validation rules
for (Schema.SObjectField field : fields.values()) {
// Check if the field has any validation rules
if (field.getDescribe().isNillable() == false) {
// Validate the user input against the field's validation rules
if (userInput.isEmpty()) {
// Handle validation error
}
}
}
}
}
Best Practices and Architectural Patterns
To ensure a seamless user experience, it’s essential to follow best practices and architectural patterns when building Agentforce conversational flows that enforce Salesforce validation rules.
Heads up: When using Apex to validate user input, make sure to handle errors and exceptions properly to avoid disrupting the conversational flow.
Checklist for Enforcing Salesforce Validation Rules in Agentforce
- Use Apex describe metadata to retrieve validation rules for the object
- Iterate through the fields and validate the user input against each field’s validation rules
- Handle validation errors and exceptions properly
- Use Topic Instructions and Action Input Instructions to refine conversational user inputs
- Test and iterate on the conversational flow to ensure a seamless user experience
- Monitor and analyze user interactions to identify areas for improvement
What is the recommended pattern to evaluate Salesforce validation rules before creating records in an Agentforce flow?
The recommended pattern is to handle error checks in the automation performing the DML or have a validation action that checks for any validations necessary before calling another action that will do the commit.
Can I use Apex to validate user input against Salesforce validation rules?
Yes, you can use Apex to validate user input against Salesforce validation rules by leveraging Apex describe metadata and validation rule evaluation.
How can I surface validation feedback back to the user during the conversation?
You can surface validation feedback back to the user during the conversation by using Topic Instructions and Action Input Instructions to refine conversational user inputs and handle validation errors and exceptions properly.
What are some best practices for building Agentforce conversational flows that enforce Salesforce validation rules?
Some best practices include using Apex describe metadata, iterating through fields and validating user input, handling validation errors and exceptions properly, and testing and iterating on the conversational flow.
Need help shipping this in production?
Genetrix builds and untangles Salesforce Marketing Cloud and Agentforce setups for teams that want it done right the first time. If anything in this post sounds familiar, talk to us before it ships.