- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2023 09:16 AM
Hello All,
I am trying to populate key-value pair from multi row variable set of incident table. With below code I am getting JSON array
var gr = new GlideRecord('incident');
gr.addQuery('number=INC0001164');
gr.query();
if (gr.next()) {
var mrvs = gr.variables.employee_details;
gs.print(mrvs);
}
*** Script: [ { "name" : "7db62c631b655150ded9eca8ab4bcb90", "number" : "007810", "manager" : "ABC", "cost" : "01394040" } ]
If I try to parse the multi row variable set (var Payload = JSON.parse(mrvs); gs.print(Payload);) I am getting below result
Script: [object Object]
Can somebody help to get only key value pair from the JSON array. I am using this code in template script that needs to be populated on the ticket
The format should look like below
Name: ABC
Number: INC0001164
Manager: XYZ
Thanks,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2023 09:22 PM
Hello Reshma,
Using the following code, you can iterate through the JSON array and print the key-value pairs:
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0001164');
gr.query();
if (gr.next()) {
var mrvs = gr.variables.employee_details;
var Payload = JSON.parse(mrvs);
for (var i = 0; i < Payload.length; i++) {
var obj = Payload[i];
var keys = Object.keys(obj);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
if (key == "name") {
gs.print("Name: " + obj[key]);
}
if (key == "manager") {
gs.print("Manager: " + obj[key]);
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2023 07:55 PM
It looks like that mrvs is already a json object, you don't need to use JSON.parse(mrvs) to convert it to a json object.
Try to such as use mrvs[0] .name to get the name...
Have a try !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2023 09:22 PM
Hello Reshma,
Using the following code, you can iterate through the JSON array and print the key-value pairs:
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0001164');
gr.query();
if (gr.next()) {
var mrvs = gr.variables.employee_details;
var Payload = JSON.parse(mrvs);
for (var i = 0; i < Payload.length; i++) {
var obj = Payload[i];
var keys = Object.keys(obj);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
if (key == "name") {
gs.print("Name: " + obj[key]);
}
if (key == "manager") {
gs.print("Manager: " + obj[key]);
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2023 11:00 PM
if (key == "name") {
gs.print("Name: " + obj[key]);
}
While using above code I am getting sys id of the field. How can I get the display value?
I have already tried getdisplayvalue() and tostring() methods it is giving me null value
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2023 09:36 PM - edited ‎02-06-2023 09:38 PM
Hi @Reshma77 ,
can you check below it will help you to get the data and use wherever it is required.
https://www.servicenow.com/community/developer-forum/get-values-from-mrvs/m-p/1864581
ServiceNow Community MVP 2024.
Thanks,
Pavankumar