setAbortAction not working with GlideAjax

tahnalos
Kilo Sage

We had put in two business rules to limit the actions of Demands so that the business properly completes them before closing the Demand.  Specifically, the Demand needs to be properly Approved before it is Completed.  And close notes are required when completing a Demand.

 

However, when we attempt to create a project from a Demand, we are noticing that the Demand is automatically set to complete.  Naturally we thought the business rules would prevent creation of the project only to discover that is not the case:

tahnalos_0-1735873202318.png

It would appear that the "Create Project" link uses a GlideAjax to not only create the project, but to set the Demand to Complete.  The question is whether setAbortAction (true) does not cover situations when a GlideAjax is initiated.

 

Advice on how to prevent the project entry from being created?

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@tahnalos 

you should validate it before making the GlideAjax call itself.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@tahnalos 

I have highlighted the part where you will have to perform the validation and stop user

var gDialog;

function convertDemandToProject() {
// handle your validation here if it is fine then proceed and if not then show message return
 
    if (g_form.modified) {
        return g_form.addErrorMessage(getMessage('You have not saved all your changes, save the demand record before creating project'));
    } if(g_form.getValue('type') == 'project'&& g_form.getElement("calculation_type") && !g_form.getValue('calculation_type')) {
        return g_form.addErrorMessage(getMessage('Please select a project calculation type under preferences.'));
    }
    var ga = new GlideAjax("AjaxCreateRelatedEntityFromDemand");
    ga.addParam("sysparm_name", "checkPmoUpgrade");
    ga.addParam("sysparm_source_table", g_form.getTableName());
    ga.getXML(function(response) {
        var result = response.responseXML.getElementsByTagName("result");
        var isUpdrading = result[0].getAttribute("isUpgrading");
        var isProjectCurrencyEnabled = result[0].getAttribute("isProjectCurrencyEnabled");
        var isMoreThanOneProjectClassExists = result[0].getAttribute("isMultipleProjectClassExists");
        var defaultProjectTable = result[0].getAttribute("defaultProjectTable");
        if (isUpdrading === "true") {
            g_form.addErrorMessage(getMessage("Financial Planning Upgrade Job Running. Cannot create Project."));
        } else if (isProjectCurrencyEnabled == "true" || isMoreThanOneProjectClassExists == "true") {
            gDialog = new GlideModal('demand_to_project');
            gDialog.setPreference('sysparm_task_id', g_form.getUniqueValue());
            gDialog.setPreference('sysparm_projTable', defaultProjectTable);
            gDialog.setPreference('sysparm_isProjectCurrencyEnabled', isProjectCurrencyEnabled);
            gDialog.setPreference('sysparm_isMoreThanOneProjectTableExists', isMoreThanOneProjectClassExists);
            gDialog.setPreference('on_submit', onSubmit);
            gDialog.setPreference('on_cancel', onCancel);
            gDialog.setTitle(new GwtMessage().getMessage('Create project'));
            gDialog.render();
        } else
            onSubmit(defaultProjectTable);
    });
}
if (typeof window == 'undefined')
    redirect();

function redirect() {
    action.setRedirectURL(current);
}

function onCancel() {
    if (gDialog)
        gDialog.destroy();
    return false;
}

function onSubmit(projName) {
    var createProjectAjax = new GlideAjax("AjaxCreateRelatedEntityFromDemand");
    createProjectAjax.addParam("sysparm_name", "createProjectAjax");
    createProjectAjax.addParam("sysparm_sys_id", g_form.getUniqueValue());
    createProjectAjax.addParam("sysparm_projName", projName);
    createProjectAjax.getXML();

    if (gDialog)
        gDialog.destroy();
    g_form.save();

    return false;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

Hi @tahnalos ,

The issue you're encountering arises because the setAbortAction(true) method in a business rule prevents direct user-initiated actions, such as form submissions, but it does not inherently block backend updates triggered by a GlideAjax script. When creating a project from a Demand, the Create Project link likely uses a GlideAjax script to set the Demand's state to "Complete," bypassing the usual checks. To enforce proper validation, you need to add backend checks to ensure the rules are respected regardless of how the update is initiated.

You can create a Before Update Business Rule on the demand table to prevent the state from being set to "Complete" unless the Demand has been approved and close notes are provided. This approach will apply universally, covering updates triggered by both interactive and backend operations.

The Before Update Business rule is what I had done and is what contains the error message and the setAbortAction.  However, the Create Project link is a UI action and is what is triggering the GlideAjax.  What you're saying makes sense if one thinks that the setAbortAction does not cover the GlideAjax scope.

Ankur's suggestion to validate before the GlideAjax call may be what I need to do, in other words, the UI action itself may need to be modified to validate before the meat of the UI action code is executed.  I need to talk to the business as to how we may accomplish this.

 

Thanks