Need help for JSON formatting

Vidya Shree
Kilo Sage

Hi All,

 

I am trying to fetch the variables and it's values populated while creating record producer in a server script.

Using GlideRecord on the Record producer base table is not working. So, I am using GlideRecord on 'sc_multi_row_question_answer' to get the values. Currently I am getting below response

 

{
"preImplementationPlan ": [
{
"duration2": "3 hrs"
},
{
"implementation_plan2": "testing plan"
},
{
"teams2": "NOC"
},
{
"teams2": "db team"
},
{
"duration2": "2 hrs"
},
{
"implementation_plan2": "testing db team"
}
]
}

But expected response is as below

 

{
"preImplementationPlan ": [
{
"implementation_plan2": "testing plan",
"teams2": "NOC"
"duration2": "3 hrs"
},
{
"implementation_plan2": "testing db team",
"teams2": "db team",
"duration2": "2 hrs"
}
]
}

Below is the code that i am using

 

var Arr = [];
var test = {};
var mrvs = new GlideRecord('sc_multi_row_question_answer');
mrvs.addQuery('parent_id', '5ef3c42593ba4210d783bba97bba10b0');
mrvs.addQuery('variable_set','ab4ea5b4937a4210d783bba97bba10a7');
mrvs.groupBy('row_index');
mrvs.query();
while (mrvs.next()) {
var preImplementationPlan = {};
 preImplementationPlan[mrvs.item_option_new.name.toString()] = mrvs.getValue('value');
Arr.push(preImplementationPlan);
}
test["preImplementationPlan "] = Arr;
 
gs.addInfoMessage(JSON.stringify(test));

 

 

 

 

1 ACCEPTED SOLUTION

Hi @Kieran Anson ,

 

Thank you for explaining with example. But I'm trying to get the variables from the record producer.

I found the solution. I'm posting it below.

 

var Arr = [];
var test = {};
var mrvs = new GlideRecord('sc_multi_row_question_answer');
mrvs.addQuery('parent_id', '5ef3c42593ba4210d783bba97bba10b0');
mrvs.addQuery('variable_set', 'ab4ea5b4937a4210d783bba97bba10a7');
mrvs.orderBy('row_index'); // Ensure that results are ordered by row_index
mrvs.query();

var currentRowIndex = null;
var preImplementationPlan = {};

while (mrvs.next()) {
var rowIndex = mrvs.getValue('row_index');
if (currentRowIndex !== rowIndex) {
if (currentRowIndex !== null) {
Arr.push(preImplementationPlan);
}
preImplementationPlan = {};
currentRowIndex = rowIndex;
}
preImplementationPlan[mrvs.item_option_new.name.toString()] = mrvs.getValue('value');
}

// Add the last set of variables
if (Object.keys(preImplementationPlan).length > 0) {
Arr.push(preImplementationPlan);
}

test["preImplementationPlan"] = Arr;

gs.addInfoMessage(JSON.stringify(test));

 

View solution in original post

6 REPLIES 6

Personally wouldn't use this approach, as it's not conforming with how access is intended

BillMartin
Mega Sage

Hi @Vidya Shree ,

 

Sharing a sample code that I have created, where I drilled down the object from a parent to a child key-value pair until I got the value. Using multiple JavaScript techniques with a combination of ServiceNow objects.

 

Btw, this is not production code, I created it for a demo purpose to fetch and display the values from the server to the client side.

 

Screenshot 2024-06-18 at 8.32.31 AM.png

 

Screenshot 2024-06-18 at 8.35.47 AM.png

 

You can watch the details from 36:28 to 48:40

 

Here is Where: Scripting in ServiceNow Fundamentals

 

Please mark as helpful and accept as solution if you find it lucrative.