- 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-02-2020 12:16 AM
Yes, this much closer but without the grouped braces.
{"requested_for":"Abel Tuter","requested_by":"Sam Jone","model":"Hyundai","quantity":"1","device_model_name":"Car-Petrol"}
For the fields to be parsed on the client side, wouldn't I need to have the braces removed or should the braces not be a factor? There will be at least 20 field values that I would need to fill in.
I think the client script you edited accounts for a single value and not all values in the array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2020 12:54 AM
Hi,
if you want to return 20 values then you can directly return an array of values; no need of json object
you can then use the array to set the values
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-02-2020 08:48 PM
I seem to be getting an undefined error as a return.
I'm not too clear about the loop. The loop seems that it would be finding the first instance of supplier_name and stopping. How would I return all the different field information individually?
I'm looking to pull all unique field names and their variables from a previous RITM to fill in the appropriate field in a new catalog item. There are more fields and variables in the my array instead of just a single one. The "supplier_name:answer" is just one of many. I apologize if that wasn't clear.
Ex: a previous RITM would contain the following. On the form, the user would select a previous RITM and the script would fill in all the values of the past RITM into a new form.
My array would have this kind of data:
name:John
lastname:Smith
city:New York
state:NY
zip: 10001
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2020 08:58 PM
Hi,
So what I assume is the user will select RITM of same catalog item and variables from previous RITM needs to be set on new RITM?
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-02-2020 09:09 PM
Yes, that is correct. A field is already present to provide the option to select an RITM by number.
I think my options are either to loop to return the same name:variable combinations in the array or hard code each answer like the below so I can pick and choose which fields to be filled in.
The goal is to reduce the work for the end user if the RITM is rejected due to a small error.