When building complex validations in Creatio, it can sometimes
be better to optimize the validation rules using code. As an example, we recently
needed a validation that would check for certain data conditions in a related
object when an Activity was saved. Specifically, we needed the Activity's
Category lookup to use only certain values when its account had a particular
"Type" value.
Different Activity Category values used in the past are not
applicable in the future, but we still want to allow these values to be present
for backwards compatibility. However, for any future data being saved, the
system needs to alert the user via a popup so it is clear to them that some
values are not acceptable going forward.
Though the Creatio business rule designer uses its no code
approach to support functionality that is somewhat close to what we needed,
after considering the necessary changes, it did not provide the ideal
experience. So, the coded approach I’ll outline in this article seemed to be a
better fit.
How the Activity Page in
Creatio Should Work
Our client in this example works in the healthcare industry.
When an activity is created/modified for an account that is of type
"Physician Sales", the system should not allow the activity to be
saved if the activity category is not Email, Phone Call, or Meeting.
To implement the code, we need to first open the page and
make sure our changes are being implemented into the "methods"
section of the page schema.
In order for our custom code to be hooked up to the base
functionality that Creatio provides, we must add the code to a validation
method that it recognizes. This can be done using the asyncValidate method,
which is generally available on all edit pages. This method is where you can
add the necessary code that handles the custom validation logic. When the validation
completes, it is important to make sure the proper result is returned to the
page so that it knows how to respond to the user.
Note that the result is structured as an object that
contains a success/failure flag along with a message. So, the appropriate
object must be returned depending on the situation:
- If validation found no issues, return this: {success:true, message: ""}
- And if there is a problem, return this: {success:false, message: "Something does not look right. Please correct…"}
Below is the JavaScript code you would add to the "methods" area which contains the necessary logic that checks the Account type and then checks whether the activity category is Email, Phone Call or a meeting.
The asyncValidate method can also be added to the
"ActivityMiniPage" schema to perform the same validation when an
activity is being created. If the mini page is disabled, then this step is not
required.
Once implemented on the Activity page, the save operation
will call it and check if it meets the specified validation rules. If the
Account Type is "Physician Sales" and Category of the activity is not
"Phone Call", "Meeting" or "Email", then the
validation will refuse to save it with the following message:
The same approach can be extended to add more complex
validation logic to ensure the data being input is always meeting the
ever-increasing needs of your business.