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

Hi,

updated the script just now; please check latest script

Regards
Ankur

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

Hi,

please update script include and client script as below

it would be easier if the json looks like this so that it becomes easier to parse

{"requested_for":"Fred Luddy","requested_by":"Amy Jone","model":"Honda","quantity":"1","device_model_name":"Car-Diesel"}

Script Include:

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 obj = {}

			for (var prop in reqTable.variables) {
				if (reqTable.variables.hasOwnProperty(prop)) {
					var variable = reqTable.variables[prop];
                                        // push to object if variable value is not empty
                                        if(variable != ''){
					obj[prop] = variable.toString();
                                        }
				}
			}
			return JSON.stringify(obj);
		}
	},

Client Script:

 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); // convert json string to json object
			var keys = Object.keys(parser); // get all keys from json object
			for(var i=0;i<keys.length;i++){

				var variableName = keys[i];
				var variableValue = parser[keys[i]];
				g_form.setValue(variableName,variableValue);
				
		}
	}
}

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

Hi Ankur,

This is awesome I am reusing it. I have a question would it be possible to exclude a variable to be copied?

Many Thanks!

In case you are unavailable I have opened a new thread

 

https://community.servicenow.com/community?id=community_question&sys_id=73e57d70db30dd10770be6be13961905

Eli

Thank you. The array was definitely the issue. Sorry for not clarifying the goal.