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

try now

 

Script Include:

 

getReqFields: function() {
        
        var str = [];
        var item = this.getParameter('sysparm_request');
        var reqTable = new GlideRecord('sc_req_item');
        reqTable.get(item);

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

                var json = new JSON().encode(str);
		return json;
    },

 

execute your client script and see what are you getting now. 

 

 

Still getting prop and variable labels and each is enclosed in brackets. I don't need the "prop" and "variable" in the array output.

[{"prop":"supplier_request_type","variable":"existing"},

Can I still parse out my results even if each set is brackets with my current client script?

just confirming , do you need variable name and variable value in json ? 

 if yes then try with ankur solution .

 

 

 

 

Hi,

please check the script shard in my comment; I believe that is what is required by you.

Regards
Ankur

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

Hi,

So are you saying if you have 5 variables in the RITM then you want to see the json as below

{"variableName":"variableValue"}

[{"requested_for":"Abel Tuter"},{"requested_by":"Sam Jone"},{"model":"Hyundai"},{"quantity":"1"},{"device_model_name":"Car-Petrol"}]

If yes then please update code as below

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 arr = [];

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

		}
	},

Also update client script as below; I assume you only want supplier_name variable

variable name -> supplier_name

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 parser = JSON.parse(answer);
			var supplierValue = '';
			for(var i=0;i<parser.length;i++){
				if(parser[i].supplier_name != ''){
					supplierValue = parser[i].supplier_name;
					break;
				}
			}
			
			g_form.setValue('supplier_name', supplierValue);

		}
	}

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