pengz
pengz

Reputation: 2471

ServiceNow Client Script asynchronous query to stop submission onSubmit in new Service Portal

I have a Client Script that performs a GlideRecord query to check if a record already exists with the same name.

If a matching record is found, we need to STOP the form from being submitted.

We have this working fine on the CMS portal.

However, the new Service Portal does not support synchronous GlideRecord query.

So I can't use gr.query() I need to use a callback such as gr.query(callback).

The issue is that since the callback is asynchronous, it does not actually stop the form from being submitted!

g_form.submitted = false; DOES NOT work. That's because the script proceeds along to submit the form before the callback has a chance to retrieve the value.

How can we stop the submission of a form based on the value returned by an asynchronous callback? We can't use GlideAjax for the same reason, getXMLWait() is no longer supported.

Here is the client script that I am trying to get working in the new Service Portal.

function onSubmit() {

    var group_name = g_form.getValue('u_group_name');
    g_form.hideAllFieldMsgs('error');

    /*Check if group already exists*/
    var rec = new GlideRecord('u_auth_group');
    rec.addQuery('u_group_name', u_group_name);
    rec.query(getAccountResponse);
    }

function getAccountResponse(rec) {
    while (rec.next()) {
        g_form.showFieldMsg('u_group_name', " Group Name exists already, please select another group name",'error');
        g_form.submitted = false; //DOES NOT STOP THE FORM FROM BEING SUBMITTED
        return false;
    }
}

Here is the existing script that works in the CMS portal.

function onSubmit() {

    var group_name = g_form.getValue('u_group_name');
    g_form.hideAllFieldMsgs('error');

    /*Check if group already exists*/
    var rec = new GlideRecord('u_auth_group');
    rec.addQuery('u_group_name', u_group_name);
    rec.query(getAccountResponse);
    while (rec.next()) {
        g_form.showFieldMsg('u_group_name', " Group Name exists already, please select another group name",'error');
        g_form.submitted = false; //Stops the form from being submitted if a result is returned
        return false;
    }
}

Upvotes: 0

Views: 8585

Answers (2)

pengz
pengz

Reputation: 2471

I was able to solve this by using an asyc callback with the glide record query.

function onSubmit() {
//If ServicePortal
if (!window) {
    if (g_scratchpad.isFormValid) {
        return true;
    }

    g_form.hideAllFieldMsgs("error");

    var actionName = g_form.getActionName();

    //Group Name contain letters numbers and dashes only
    var group_name = g_form.getValue("u_group_name");

    //Group name regular expression
    var regGrpName = /^([A-Za-z0-9\-]+)$/;

    //Check name against regular expression
    validGroupName = regGrpName.test(group_name);

    //Check if google group already exists
    var gg = new GlideRecord("u_system_group");
    gg.addQuery("u_group_name", group_name);

    //Callback function to control stop submit asynchronously
    gg.query(function() {       
        while (gg.next()) {
            g_form.showFieldMsg("u_group_name","Group Name " + gg.u_group_name + " already exists! Please enter a different group name.", "error", true);
            return false;
        }
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    });

    return false;
  }
}

Upvotes: 1

John Benedetti
John Benedetti

Reputation: 56

We're on Helsinki Patch 5 so we're going through similar growing pains. We've had luck using the following structure. There are still Glide System resources available Server Side, including Glide Record.

You might try wrapping your Submit action inside of a custom event handler.

Try:

Client side:

c.createGroup = function(groupName){
return c.server.get({
  grpname : groupName
}.then(function(response){
    if (response.data.result == true){
    //don't submit
    }
    else{
    //submit
    }
}

Server Side

data.result = false
data.grpname = input.grpname

function checkGroupExists(data.grpname){
    /*Check if group already exists*/
    var rec = new GlideRecord('u_auth_group');
    rec.addQuery('u_group_name', data.grpname);
    rec.limit(1); //you only need to find one match
    rec.query()
while (rec.next()){
    data.result = true
    }
}

Then you can bind this event handler to some action in the UI.

Upvotes: 1

Related Questions