The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Catalog Client Script include getting an answer but not returning it

litchick10
Tera Guru

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

 

1 ACCEPTED SOLUTION

kaushal_snow
Mega Sage

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.

 

Thanks and Regards,
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/

View solution in original post

5 REPLIES 5

Ajay_Chavan
Kilo Sage
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(','));
    });
}
Glad I could help! If this solved your issue, please mark it as Helpful and Accept as Solution so others can benefit too.*****Chavan A.P. | Technical Architect | Certified Professional*****