Not able to get the response in client script from script include

mohanambalnanja
Tera Contributor

Hi All,

 

As per the requirement, I need to capture the "Operational Status" change information in  Business Service table string field.

I am able to get the answer in script include, but when I try to take that response in Client script it is not working.

 

var OptnlStusInfo = ' ';

var ga = new GlideAjax('ReferenceDataUtilAsync'); //script include name
            ga.addParam('sysparm_name', 'getBusinessServiceOperationStatusChoice'); //function in script include
            ga.addParam('sysparm_operationalStatus', newValue);
            ga.addParam('sysparm_loggedinuser',loggedInUsr);
            ga.getXMLAnswer(StatusInfo);

        function StatusInfo(response)
{
     OptnlStusInfo = response;
    //.responseXML.documentElement.getAttribute("OptnlStusInfo");
   
}
When I print the return result in script include it is giving expected result.
I am facing issue when I call that response in client script.
 
Please help me on this .
 
Thank you. 
1 ACCEPTED SOLUTION

Deepak Shaerma
Kilo Sage

Hi @mohanambalnanja 

When working with GlideAjax and Client Scripts in ServiceNow, it’s crucial to understand that the Ajax response is asynchronous. This means that the code execution does not wait for the response before moving on to the next line. The callback function (StatusInfo in your case) handles the response once it is received, but any code that relies on the response from the server must be inside this callback function or called from within it.

From your description, it seems like the issue may be related to how you’re attempting to use the OptnlStusInfo variable after setting it within the callback function. If you’re trying to use OptnlStusInfo outside the callback function right after the getXMLAnswer call, it will not work as expected because the value would not have been updated yet.

 

var ga = new GlideAjax('ReferenceDataUtilAsync'); 
ga.addParam('sysparm_name', 'getBusinessServiceOperationStatusChoice'); 
ga.addParam('sysparm_operationalStatus', newValue);
ga.addParam('sysparm_loggedinuser', loggedInUsr);
ga.getXMLAnswer(function(response) { // Directly using anonymous function for callback
    var OptnlStusInfo = response; // This value is now scoped to this function
    processResponseStatus(OptnlStusInfo); // Call another function to process or use this info
});

function processResponseStatus(statusInfo) {
    // Process the status info here or update the UI as needed
    console.log(statusInfo); // For demonstration, let’s just log it
}

 



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 

View solution in original post

5 REPLIES 5

Deepak Shaerma
Kilo Sage

Hi @mohanambalnanja 

When working with GlideAjax and Client Scripts in ServiceNow, it’s crucial to understand that the Ajax response is asynchronous. This means that the code execution does not wait for the response before moving on to the next line. The callback function (StatusInfo in your case) handles the response once it is received, but any code that relies on the response from the server must be inside this callback function or called from within it.

From your description, it seems like the issue may be related to how you’re attempting to use the OptnlStusInfo variable after setting it within the callback function. If you’re trying to use OptnlStusInfo outside the callback function right after the getXMLAnswer call, it will not work as expected because the value would not have been updated yet.

 

var ga = new GlideAjax('ReferenceDataUtilAsync'); 
ga.addParam('sysparm_name', 'getBusinessServiceOperationStatusChoice'); 
ga.addParam('sysparm_operationalStatus', newValue);
ga.addParam('sysparm_loggedinuser', loggedInUsr);
ga.getXMLAnswer(function(response) { // Directly using anonymous function for callback
    var OptnlStusInfo = response; // This value is now scoped to this function
    processResponseStatus(OptnlStusInfo); // Call another function to process or use this info
});

function processResponseStatus(statusInfo) {
    // Process the status info here or update the UI as needed
    console.log(statusInfo); // For demonstration, let’s just log it
}

 



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma 

shaikha
Tera Guru

Hi @mohanambalnanja 

 

I see you are using getXMLAnswer() method, this method calls the processor asynchronously and gets the answer element of the response in XML format.

Note: GlideAjax with getXML still would return a whole document. While GlideAjax with getXMLAnswer would only return the exact answer you need. 
articles: https://www.servicenow.com/community/developer-articles/getxmlanswer-vs-getxml/ta-p/2307589

 

//client script code
var ga = new GlideAjax('ReferenceDataUtilAsync'); //script include name
            ga.addParam('sysparm_name', 'getBusinessServiceOperationStatusChoice'); //function in script include
            ga.addParam('sysparm_operationalStatus', newValue);
            ga.addParam('sysparm_loggedinuser',loggedInUsr);
            ga.getXMLAnswer(StatusInfo);

        function StatusInfo(response){
            var OptnlStusInfo = response;
           alert(OptnlStusInfo);
          }

 

If my response helped you, please mark this response correct by clicking on Accept as Solution and/or Helpful. It will help other who are looking for same query.

 

Regards,
Shaikha.

mohanambalnanja
Tera Contributor

Hi @Deepak Shaerma ,

 

Thanks for your response.

 

But still I am not able to get the response in client script from the script include .

 

Hi,

Could you please paste both the codes here.

 

Thanks.