Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need assistance with onsubmit client script

Community Alums
Not applicable

Hi all, 

Instead of getXML(), I want to use getXMLWait(), Can you please help, I dont knoe the syntax how to use it. FYR: answer1 is returning an array.

 

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var flagArray = [];
    var flag = 'false';
var name1 = g_form.getValue('papplication_name');
    var ga = new GlideAjax('global.ClientScriptName');
    ga.addParam('sysparm_name', 'getVariablefromItem');
    ga.addParam('sysparm_catupdte', name1);
    ga.getXML(catupdateResponse1);
    return false;

    function catupdateResponse1(response1) {
    var answer1 = response1.responseXML.documentElement.getAttribute("answer");
    answer1 = JSON.parse(answer1);
    for (var i = 0; i < answer1.length; i++) {
        var namefield = answer1[i].toString();
        if (g_form.getValue(namefield)) {
            flag = 'true';
            break;
        }
    }
    if (flag == 'false') {
        g_form.addErrorMessage('No need to submit');
        return false;
    }
    var actionName = g_form.getActionName();
    g_scratchpad.isFormValid = true;
    g_form.submit(actionName);
}
}
2 REPLIES 2

Samaksh Wani
Giga Sage

Hello @Community Alums 

 

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var flagArray = [];
    var flag = 'false';
var name1 = g_form.getValue('papplication_name');
    var ga = new GlideAjax('global.ClientScriptName');
    ga.addParam('sysparm_name', 'getVariablefromItem');
    ga.addParam('sysparm_catupdte', name1);
    ga.getXMLWait(catupdateResponse1);
    return false;

    function catupdateResponse1(response1) {
    var answer1 = response1.responseXML.documentElement.getAttribute("answer");
    answer1 = JSON.parse(answer1);
    for (var i = 0; i < answer1.length; i++) {
        var namefield = answer1[i].toString();
        if (g_form.getValue(namefield)) {
            flag = 'true';
            break;
        }
    }
    if (flag == 'false') {
        g_form.addErrorMessage('No need to submit');
        return false;
    }
    var actionName = g_form.getActionName();
    g_scratchpad.isFormValid = true;
    g_form.submit(actionName);
}
}

 

Plz mark my solution as Accept, If you find it helpful.

 

Regards,

Samaksh

Danish Bhairag2
Tera Sage

Hi @Community Alums ,

 

To use `getXMLWait()` instead of `getXML()` in your ServiceNow client script, you'll need to make some adjustments to your existing code. The `getXMLWait()` method is synchronous and will block until the response is received. Here's an updated version of your script:

 

function onSubmit() {

    if (g_scratchpad.isFormValid) {

        return true;

    }

 

    var flagArray = [];

    var flag = 'false';

    var name1 = g_form.getValue('papplication_name');

    var ga = new GlideAjax('global.ClientScriptName');

    ga.addParam('sysparm_name', 'getVariablefromItem');

    ga.addParam('sysparm_catupdte', name1);

 

    // Use getXMLWait() instead of getXML()

    var response1 = ga.getXMLWait();

    catupdateResponse1(response1);

 

    return false;

 

    function catupdateResponse1(response1) {

        var answer1 = response1.responseXML.documentElement.getAttribute("answer");

        answer1 = JSON.parse(answer1);

        for (var i = 0; i < answer1.length; i++) {

            var namefield = answer1[i].toString();

            if (g_form.getValue(namefield)) {

                flag = 'true';

                break;

            }

        }

        if (flag == 'false') {

            g_form.addErrorMessage('No need to submit');

            return false;

        }

        var actionName = g_form.getActionName();

        g_scratchpad.isFormValid = true;

        g_form.submit(actionName);

    }

}

 

 

In this version:

 

- The `getXML()` method is replaced with `getXMLWait()`.

- The `catupdateResponse1` function is still used as the callback for processing the response.

- The rest of the code remains largely unchanged.

 

Keep in mind that using `getXMLWait()` is a synchronous operation, and it may impact the user experience if there is a delay in the response. Ensure that it aligns with your application's requirements and performance considerations.

 

Thanks,

Danish