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

Kieran Anson
Kilo Patron

You're currently trying to workaround the correct way of accessing variables of a record. And in doing so you're going to cause yourself a headache.

 

How were you accessing the variables when it wasn't working? In addition, you can also use the following to get variable details in JSON format that SN intend you to use.

 

new GlobalServiceCatalogUtil.getVariablesForTask(/** task based GlideRecord **/ taskGR , /** boolean - want MRVS **/ true)

Vidya Shree
Kilo Sage

Hi @Kieran Anson ,

 

Thanks for your reply.

Basically i am trying to access the multi-row variables which are created in the record producer. Since none of the things mentioned in the community worked i am trying to fetch using glide record.

How can i use "new GlobalServiceCatalogUtil.getVariablesForTask" to fetch those multi row variables?

 

Thanks,

Vidyashree

Assuming you're trying to fetch the information post creation, the linked snippet is a call to an OOB script include.

For example

var gr = new GlideRecord('sc_req_item');
gr.get('ed49b25583de42105e43c4a6feaad363');

new GlobalServiceCatalogUtil().getVariablesForTask(gr , true)

Returns the following array

  {
    "label": "Department",
    "display_value": "Customer Support",
    "visible_summary": true,
    "multi_row": false,
    "type": 8,
    "value": "5d7f17f03710200044e0bfc8bcbe5d43"
  },
  {
    "label": "Business Justification",
    "display_value": "Test",
    "visible_summary": true,
    "multi_row": false,
    "type": 2,
    "value": "Test"
  },
  {
    "label": "Mobile Devices Set",
    "visible_summary": true,
    "multi_row": true,
    "table_variable": [
      [
        {
          "name": "device_type",
          "label": "Device Type",
          "display_value": "Apple iPhone 8"
        },
        {
          "name": "storage",
          "label": "Storage",
          "display_value": "64GB"
        },
        {
          "name": "color",
          "label": "Color",
          "display_value": "Black"
        },
        {
          "name": "quantity",
          "label": "Quantity",
          "display_value": "1"
        }
      ],
      [
        {
          "name": "device_type",
          "label": "Device Type",
          "display_value": "Samsung Galaxy s8"
        },
        {
          "name": "storage",
          "label": "Storage",
          "display_value": "64GB"
        },
        {
          "name": "color",
          "label": "Color",
          "display_value": "Black"
        },
        {
          "name": "quantity",
          "label": "Quantity",
          "display_value": "1"
        }
      ]
    ]
  }
]

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));