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

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 ?

 

By using below code while submitting record i am getting page not reponding error?

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'
});

 

2 REPLIES 2

Hemanth M1
Giga Sage
Giga Sage

Hi @Narayana Swamy ,

 

Are you using this in Service portal?? if not you can use getXMLWait

 

If you would like use this in Service portal, Please a refer this article by Willem.

https://www.servicenow.com/community/developer-articles/getxmlwait-alternative-for-service-portal/ta... 

 

 

 
Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

thomaskennedy
Tera Guru

 i am getting page not reponding error

Consider what will happen here:

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

    function getResponse(response) {
        ...
    }

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

 

You set scratchpad to false at (1).

You request a callback at (2). The getResponse function will be handed the result of the getXml() call, and placed on the stack, and run, eventually, once the stack is empty.*

Your code immediately proceeds to (3) and enters an infinite loop. The stack can never be empty, because this function will sit on it forever. This is where your error is coming from, but that is secondary.

 

I'm tolerably sure the submit just goes ahead at this point, if onSubmit doesn't stop it in a timely way.

 

Usually, the way to do whatever you were doing in (waiting for the response) is to move it into your response handler (getResponse).  But in this case, because you need to preempt the submit, you should switch to the synchronous version of getXml and drop the callback.

 

* In JavaScript you get only one thread for your own use, and your callback (getResponse) cannot use that thread until your function -- onSubmit  -- has completed and been popped off the stack. So although getResponse is declared inline, it cannot run until sometime after onSubmit has completed.