In onsubmit client script i can able to get value from glide ajax but that value is not assiging? t

Narayana Swamy
Tera Contributor

In onsubmit client script i can able to get value from script include but can't able to assign value to field i don't know what is issue please help out ?

 

code is as below:-

 

 

client script:-

function onSubmit() {

 

 

    var glideDateTime = new GlideAjax("DOMSPOAjaxUtils");

    glideDateTime.addParam("sysparm_name", "currentDateTime");

    //glideDateTime.getXMLWait();

    glideDateTime.getXML(getResponse);

    //var answer = glideDateTime.getAnswer();

 

    function getResponse(response) {

        alert('Hi121');

        var answer = response.responseXML.documentElement.getAttribute("answer");

 

        if (g_form.getValue('state') == '2') {

            alert('Hi' + answer);

            g_form.setValue('u_hold_start', answer);

            g_form.setValue('u_hold_end', '');

 

        } else {

            if (g_form.getValue('u_hold_start') != '') {

                g_form.setValue('u_hold_end', answer);

            }

        }

    }

}

 

script include:-

 

var DOMSPOAjaxUtils = Class.create();

DOMSPOAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    currentDateTime: function() {

        var glideRecord = new GlideDate();

        var gDate = glideRecord.getByFormat('MM/dd/YYYY hh:mm:ss a');

  gs.log('Hi3242342'+gDate);

        return gDate;

    },

 

    type: 'DOMSPOAjaxUtils'

});

13 REPLIES 13

Sandeep Rajput
Tera Patron
Tera Patron

@Narayana Swamy GlideAjax call is an asynchronous operation and if you are using it in the onSubmit client script it is quite possible that the form is already submitted by the time response comes from the Server, hence the values you are setting using g_form.setValue are not reflecting on the form.

 

If you wish to perform some validation on onSubmit using a client script, I recommend you refer to the article https://www.servicenow.com/community/developer-articles/async-validation-in-onsubmit-catalog-client-... and apply the same approach.

Tai Vu
Kilo Patron
Kilo Patron

Hi @Narayana Swamy 

The issue is most likely because the submit action occurred before the value was returned in your GlideAjax call. These lines indicate that g_form.setValue was not reached.

It appears that you're setting the value of the u_hold_end field based on the state field. To address this, you can change your client script from OnSubmit to OnChange on the State field.

 

Cheers,

Tai Vu

 

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @Narayana Swamy ,

 

The issue you're facing is due to the asynchronous nature of GlideAjax. The g_form.setValue is being executed before the GlideAjax response is received. To resolve this, you need to ensure that the form submission waits until the GlideAjax response is received. Here's how you can do it:

 

Client Script: 

function onSubmit() {
g_scratchpad.flag = false;
var glideDateTime = new GlideAjax("DOMSPOAjaxUtils");
glideDateTime.addParam("sysparm_name", "currentDateTime");
glideDateTime.getXML(getResponse);

function getResponse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (g_form.getValue('state') == '2') {
g_form.setValue('u_hold_start', answer);
g_form.setValue('u_hold_end', '');
} else {
if (g_form.getValue('u_hold_start') != '') {
g_form.setValue('u_hold_end', answer);
}
}
g_scratchpad.flag = true;
}

while(!g_scratchpad.flag) {
// Waiting for the response
}
return true;
}

Script Include: 

var DOMSPOAjaxUtils = Class.create();
DOMSPOAjaxUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
currentDateTime: function() {
var glideRecord = new GlideDate();
var gDate = glideRecord.getByFormat('MM/dd/YYYY hh:mm:ss a');
return gDate;
},
type: 'DOMSPOAjaxUtils'
});

 

Kindly mark the answer ✔️Correct or Helpful ✔️If it addresses your concern.


Regards,

Siddhesh

Hii @Narayana Swamy ,

 

Does this resolved you issue ? If yes, It would be great if you mark my response as CORRECT or Helpful so that others can see this on top of the list and get benefited by this.

 

Thanks & Regards,

Siddhesh

Narayana Swamy
Tera Contributor

Hi @Siddhesh Gawade ,

By using the above code i can't able to submit the form it is giving page not responding issue.