Note: This is an advanced developer feature only available for customers on the Enterprise plan.
Custom Validations let you programmatically define granular rules for what data is required for updating or creating specific record types. They are very useful for ensuring data quality at scale.
They share lots in common with Workflows - in that they rely heavily on custom JavaScript modules - but are a different concept. The two systems do integrate with each other however: a Custom Validation can suggest a Workflow to the user when a Custom Validation error is triggered. This is useful for blocking users and prompting for e.g. manager approval before proceeding with updating a record.
Creating a custom validation
Custom validations can be created by navigating to "Settings > Custom Validation" in Rex and are set up the same way as a workflow when it comes to trigger
and record type
fields.
A custom validation is written as a CommonJS JavaScript module and can be extremely powerful when used to suggest a workflow.
Your JavaScript for a Custom Validation should be an async
function that returns true
if the custom validation should NOT block, or false
if it SHOULD block.
For example, below is a Custom Validation that prevents publishing of a listing until a Listing Owner is attached.
The settings would look like this:
And the JavaScript would look like this. Note that the lodash module is available for import in all Custom Validations for convenience.
const _ = require("lodash");
module.exports = async function (context, sendRequest) {
const {changes, pre, post} = context;
// Get related contacts from listing
const relatedContacts = _.get(context, "post.related.contact_reln_listing", []);
// Check to see if we're publishing a listing.
// Our custom validation trigger is set to "Update",
// but we don't want to check ALL updates... only updates
// where the user is changing listing from Draft -> Published
const prePublicationStatus = _.get(context, "pre.system_publication_status", null);
const postPublicationStatus = _.get(context, "post.system_publication_status", null);
const isPublishing = prePublicationStatus === "draft" && postPublicationStatus === "published";
if (!isPublishing) {
return true;
}
// Check to see if at least 1 Listing Owner has
// been added to the sidebar
let hasVendor = false;
relatedContacts.forEach(contact => {
if (contact.reln_type.id === "owner") {
hasVendor = true;
}
});
return hasVendor;
};
Common patterns
A common pattern for custom validations is to combine a custom validation with a workflow by using the "Suggested Workflow" selection to allow your custom validation to prompt the user to fire a workflow.
This enables you to make complex decisions before a workflow is fired, and even block updates to records if they choose to ignore the message.
Debugging
The easiest way for a fast feedback loop to debug a custom validation is to simply return
the information you'd like to view, rather than return
ing a boolean
.
This will immediately be displayed as the error message for a custom validation. Using JSON.stringify
in conjunction with this would let you use the Custom Validation error messages as a basic console.log
equivalent during development