- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 01:22 AM
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 = ' ';
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 02:16 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 02:16 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 03:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 03:46 AM
Hi @Deepak Shaerma ,
Thanks for your response.
But still I am not able to get the response in client script from the script include .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-26-2024 04:04 AM
Hi,
Could you please paste both the codes here.
Thanks.