- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2020 09:04 PM
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);
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2020 09:27 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2020 09:27 PM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2020 10:02 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2020 10:05 PM
Replace : with ;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2020 10:05 PM
Hi,
mistake in my script; it should be semicolon and not colon
so use ; and not :
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-01-2020 10:21 PM
Not working either way unfortunately:
This is my output