How to stop action set execution when client side validation fails

I recently had a requirement where a Confirmation Local Action needed to be displayed only after all mandatory fields on the current screen had successfully passed client-side validation.

The challenge was that the button’s action set contained multiple actions, and even when client-side validation failed, the subsequent actions in the action set, including the Confirmation Local Action, continued to execute.

To address this, I created the JavaScript function below and invoked it from a Run Script action before the Local Action is executed. The script performs client-side validation, moves the focus to the first invalid field, and clears the remaining actions from the action queue if validation fails. This ensures that no further actions are executed until all validation errors are resolved.

You can add this function to an existing JavaScript text file in your application and call it from the button’s action set before the Confirmation Local Action.

Code:

function validateBeforeConfirmation() {

    window.isClientValidationPassed = false;

    if (typeof bClientValidation !== "undefined") {
        bClientValidation = true;
    }

    if (typeof validation_validate === "function") {

        if (!validation_validate()) {

            if (typeof docFocus !== "undefined") {
                docFocus.focusToFirstInvalidField();
            }

            window.isClientValidationPassed = false;

            /* Stop all remaining actions in the action set */
            if (typeof pega !== "undefined" &&
                pega.control &&
                pega.control.actionSequencer &&
                typeof pega.control.actionSequencer.clearQueue === "function") {

                pega.control.actionSequencer.clearQueue();
            }

            return false;
        }
    }

    window.isClientValidationPassed = true;

    return true;
}
2 Likes