I want to use the Answer value outside the _handleResponse
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2022 12:57 PM
I need to write a long script with the values I get using getXML
However, if we go outside the function, the value obtained by getXMLAnser will disappear
How can I keep the Answer value outside of function _handleResponse?
var ga = new GlideAjax('callerUtilsAjax');
ga.addParam('sysparm_name', 'getComputer');
ga.addParam('sysparm_caller', g_form.getValue('called_id'));
var asnwer2 =''
ga.getXMLAnswer(_handleResponse);
function _handleResponse(answer) {
console.log(answer); <- work
answer2 = answer
}
console.log(answer) <- dont work
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-14-2022 03:14 PM
You need to add the rest of your logic inside of your _handleResponse function as you don't have access to that data until this function executes anyway as it's waiting on the response from the server to do something.
Not sure why you would need to do it "outside" the function anyway.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2022 08:11 AM
The only way to achieve this is by doing the GlideAjax synchronously, which is deprecated.
If you want to do a GlideAjax "onSubmit" (Client Script), then you can use the following pattern to still get the same result while even using asynchronous GlideAjax:
function onSubmit() {
if (g_scratchpad.my_validation == 'in_progress') {
return false;
} else if (g_scratchpad.my_validation == 'success') {
g_scratchpad.my_validation = '';
return true;
}
g_scratchpad.my_validation = 'in_progress';
var currentAction = g_form.getActionName();
var ga = new GlideAjax('x_my_scope.MyScriptInclude');
ga.addParam('sysparm_name', 'myValidationFunction');
ga.addParam('my_table', g_form.getTableName());
ga.addParam('my_sys_id', g_form.getUniqueValue());
ga.getXMLAnswer(function (result) {
if (result == 'true') {
g_scratchpad.my_validation = 'success';
g_form.submit(currentAction);
} else {
g_scratchpad.my_validation = '';
g_form.addErrorMessage('Validation failed');
}
});
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-15-2022 08:37 AM
Hi @kenken1 ,
Can you share the business requirement and script that you are working?
ServiceNow Community MVP 2024.
Thanks,
Pavankumar