Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Output to an object array

KB15
Giga Guru

I can I output this loop to an object? I think this is my issue with JSON not being able to parse this out in a client script.

The loop is just combining the results from "prop" and "variable" into an array.

 

    getReqFields: function() {
        var item = this.getParameter('sysparm_request');
        var reqTable = new GlideRecord('sc_req_item');
        reqTable.addQuery('sys_id', item);
        reqTable.query();

        if (reqTable.next()) {

            var str = [];

            for (var prop in reqTable.variables) {
                if (reqTable.variables.hasOwnProperty(prop)) {
                    var variable = reqTable.variables[prop];
					
                    str.push('\"' + prop + '\":\"' + variable + '\"');
                }
            }
	
		return JSON.stringify(newStr);

        }
    },

Trying the below doesn't provide the variable data. I thought I could simply combine the results into an array and then try to reparse it. 

str.push(prop);
str.push(variable);

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I would recommend doing this so that you can easily parse it later

getReqFields: function() {
        var item = this.getParameter('sysparm_request');
        var reqTable = new GlideRecord('sc_req_item');
        reqTable.addQuery('sys_id', item);
        reqTable.query();

        if (reqTable.next()) {

            var str = [];

            for (var prop in reqTable.variables) {
                if (reqTable.variables.hasOwnProperty(prop)) {
                    var variable = reqTable.variables[prop];
		    var obj = {};
                    obj["prop"] = prop.toString();
                    obj["variable"] = variable.toString();
                    str.push(obj);			
                    
                }
            }
	
		return JSON.stringify(str);

        }
    },

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

28 REPLIES 28

Thought I'd try something different but the script provided unfortunately does not work:

find_real_file.png

Can you share client script code snippet as well.

I'm only trying to return a single value for now until I can get this script working.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //variables
    var reqItem = g_form.getValue('previous_request');

    var ga = new GlideAjax('getSupplierRequestVar'); //script include
    ga.addParam('sysparm_name', 'getReqFields'); //call function
    ga.addParam('sysparm_request', reqItem);
    ga.getXML(stateCallback);

    function stateCallback(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
		var returneddata = JSON.parse(answer);
		
		alert(returneddata);
		
        g_form.setValue('supplier_name', returneddata.supplier_name);

    }
}

Hi,

you are doing alert with JSON.parse() so it shows object

if you do alert like this; you will see the json string

function stateCallback(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        alert('JSON String is: ' + answer);
	var returneddata = JSON.parse(answer);	
		
        g_form.setValue('supplier_name', returneddata.supplier_name);

    }

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I was thinking I could see the output after it's parsed that way.

The formatting doesn't seem to fit for the way the client script is set up to parse out. It appears as:

{"prop":"name","variable":"value"},{"prop":"name","variable":"value"}

The output is grouped and shows the name and the variable. I believe it should just simply show each entry as:

{"name":"value","name":"value"}