Validating Multiple Asynchronous Results in Creatio

In a previous article I showed how to asynchronously validate a result when saving a page in Creatio. This is needed when validating something when a page is saved and the validation requires an asynchronous function, such as an EntitySchemaQuery. At the end of that article, I mentioned how you would go about doing this for validating multiple asynchronous results at the same time by using Terrasoft.chain to chain all the asynchronous validation function together. In this article I will describe how to do this in detail.

Refer to the original article for Asynchronous Validation on Pages in Creatio

In order to do this, it is easiest if you split up each asynchronous validation into it’s own function. This function would look like this:

validateThing1: function(callback, scope) {
    var result = {
        success: true,
        message: ""
    };
 
    // do whatever async call to validate and then call below to return the result, 
    // setting result.success = true if validation passed or result.success = false 
    // if failed. If the validation failed, also set the message (will be displayed to user)

    callback.call(scope || this, result);
}

Then, in the asyncValidate function itself, you’ll use Terrasoft.chain to chain all the validation function together. In this scenario, I have two validation functions, structured like the sample above, that are named validateThing1 and validateThing2.

asyncValidate: function(callback, scope) {
    this.callParent([function(response) {
        if (!this.validateResponse(response)) {
            return;
        }
        Terrasoft.chain(
            function(next) {
                this.validateThing1(function(response) {
                    if (this.validateResponse(response)) {
                        next();
                    }
                }, this);
            },
            function(next) {
                this.validateThing2(function(response) {
                    if (this.validateResponse(response)) {
                        next();
                    }
                }, this);
            },
            function(next) {
                callback.call(scope, response);
                next();
        }, this);
    }, this]);
}

Now, when the page is saved, each validation function will be chained, each executing the next, before the final result is obtained to determine if the validation was successful or not.

Want content like this delivered to your inbox? Sign up for our newsletter!
ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

9 Comments

  1. This is great. Thank you.

    Reply
  2. Do you know how this is done in Freedom UI? I have synchronous validation in place, following the Academy documentation, but they don’t have any async validation examples. In their own sync examples, you can see the “async” property set to false, but I expect there’s a little more to know than just setting that to true 🙂

    Reply
    • I’ve not tried it myself, so hard to say. I’ve looked at some of the out of the box validators that have async:true, such as “crt.UserPasswordComplexity”, used on SysUserProfilePage. I don’t really see anything different happening there, other than yield being used for some of the async calls for system settings. Maybe all it does is wrap the validator in an async function so you can await?

    • Thanks Ryan, it seems like it might just be that, though haven’t got a fully working setup going yet. Any idea on how someone might pass in an attribute from the page as a parameter to a validator though? E.g. the record’s Id to be used in querying the database for the async validator?

    • I’ve not figured out how to get other page attributes in the validator yet. There’s no context of any kind available in the validator that I’ve found. I brought this up to Creatio but haven’t heard anything back yet on how to get the page context.

    • That’s a shame, I also tried a few ways to pass in a parameter as you can with validators in Creatio, but couldn’t figure out how to bind an attribute to the parameter – doing something like “$Id” would just pass in the literal string “$Id” which is frustrating.

    • Very true. I had thought of reading the Id from the URL and using that in the validator code, but that is just hacky and not very reliable since it doesn’t always reflect the edit page context depending on how the page was opened. Each new version I dig though the ootb code to see what I can learn, I’ve not done that yet for 8.1.3 – hopefully there’s something more there.

    • Yeah, the only way I’d thought of was to use the Storage module to pass the info across, but it does seem like you should be able to get the Id of the record being validated in a nicer way without having to manage that cached data. I mean surely querying the database to check on the record’s other data (or related data) is one of the primary use cases for an async validator haha.

    • Storage would probably work, but why do we have to resort to a hacky route like this?? Not to mention, without being able to get other attributes on the page you couldn’t even do validations like a start date is before an end date etc. Hopefully they make it so we can just bind attributes for the validator or get a context passed in.

Submit a Comment

Your email address will not be published. Required fields are marked *