
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
I have a script include that pulls data from any table any fields and it is generating an answer but the return areaArr (near the end) isn't working. I put notes in the scripting to validate what has returned a value
var primeQVal = 'feda0022c3eb6210d60479bdc00131d6';
var value2 = getSlush(answer.u_product_type,'100',primeQVal);
// Confirmed answer.u_product_type is populating, confirmed function is processing (see below) but is not returning the function response;
g_form.setValue('product_type', value2); //value2 is not returning
function getSlush(idString, orderString, question) {
var areasArr = [];
var products = new GlideAjax('getTableDataUtils');
products.addParam('sysparm_name', 'getList');
products.addParam('sysparm_query_table', 'question_choice');
products.addParam('sysparm_query_fields', 'sys_id');
products.addParam('sysparm_query_encodedQuery', "valueIN" + idString + '^order=' + orderString + '^question=' + question);
products.addParam('sysparm_query_order', 'sys_id');
products.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); //Returns a value
answer = answer.replace(/},{/g, '}},{{');
var answerArr = answer.split('},{');
for (var i = 0; i < answerArr.length; i++) {
var answerObj = JSON.parse(answerArr[i]);
areasArr.push(answerObj.sys_id.toString());
}
//Confirmed areasArr has correct values
return areasArr; //DOES NOT RETURN
});
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @litchick10 ,
Your getSlush(...) function calls GlideAjax asynchronously with .getXML(...). The return areasArr; statement inside that callback doesn’t return to getSlush; instead, it returns only within the callback context, so value2 remains undefined....
You cannot return a value from an asynchronous call inside a synchronous method...
var primeQVal = 'feda0022c3eb6210d60479bdc00131d6';
getSlush(answer.u_product_type, '100', primeQVal, function(value2) {
g_form.setValue('product_type', value2);
});
function getSlush(idString, orderString, question, callback) {
var products = new GlideAjax('getTableDataUtils');
products.addParam('sysparm_name', 'getList');
products.addParam('sysparm_query_table', 'question_choice');
products.addParam('sysparm_query_fields', 'sys_id');
products.addParam('sysparm_query_encodedQuery',
"valueIN" + idString + '^order=' + orderString + '^question=' + question);
products.addParam('sysparm_query_order', 'sys_id');
products.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer"); //Returns a value
answer = answer.replace(/},{/g, '}},{{');
var answerArr = answer.split('},{');
var areasArr = [];
for (var i = 0; i < answerArr.length; i++) {
var answerObj = JSON.parse(answerArr[i]);
areasArr.push(answerObj.sys_id.toString());
}
// Invoke the callback with the processed value
callback(areasArr.join(',')); // Or keep as array if needed
});
}
The best practice is to pass a callback to handle the result...
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Try this:
var primeQVal = 'feda0022c3eb6210d60479bdc00131d6';
getSlushAndSetValue(answer.u_product_type, '100', primeQVal, 'product_type');
function getSlushAndSetValue(idString, orderString, question, fieldName) {
var areasArr = [];
var products = new GlideAjax('getTableDataUtils');
products.addParam('sysparm_name', 'getList');
products.addParam('sysparm_query_table', 'question_choice');
products.addParam('sysparm_query_fields', 'sys_id');
products.addParam('sysparm_query_encodedQuery', "valueIN" + idString + '^order=' + orderString + '^question=' + question);
products.addParam('sysparm_query_order', 'sys_id');
products.getXML(function(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.replace(/},{/g, '}},{{');
var answerArr = answer.split('},{');
for (var i = 0; i < answerArr.length; i++) {
var answerObj = JSON.parse(answerArr[i]);
areasArr.push(answerObj.sys_id.toString());
}
// Set the value directly here
g_form.setValue(fieldName, areasArr.join(','));
});
}