How to get all variables in JSON

snow_04
Giga Contributor

Hi,

We need to send variable's information for different catalog items to a 3rd party tool in JSON format. We need to extract all the variable information and need to send it in below JSON format:

{"Variables":[{"variable1 Label":"Variable 1 Value","variable2 Label":"Variable 2 Value","variable3 Label":"Variable 3 Value"}]}

Something like as shown below:

 

{
    "Variables": [{
        "variable1 Label": "Variable 1 Value",
        "variable2 Label": "Variable 2 Value",
        "variable3 Label": "Variable 3 Value"
    }]
}

 

I am able to get all variable information using the script mentioned below:

var gr = new GlideRecord('sc_task');
gr.addEncodedQuery('sys_id=d6fb6749db24c89435dc403c3a961985');
gr.query();
while(gr.next()){
for(var i in gr.variables){
if (gr.variables.hasOwnProperty(i))
{
var variable =  gr.variables[i];
gs.info(i+ ':' + variable);
}
}
}

 

Can someone please help me out in how to build this JSON structure.

 

Kindly assist!!

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

use below script to store that in the required json format

not sure I am not able to get the variables names using your script; based on sc_task so I queried sc_request_item table after your code

use below script and it will print the exact json format you want

var gr = new GlideRecord('sc_task');
gr.addEncodedQuery('sys_id=d6fb6749db24c89435dc403c3a961985');
gr.query();
if(gr.next()){

var jsonObj = {};

var ritmSysId = gr.request_item;
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(ritmSysId);
set.load();
var vs = set.getFlatQuestions();

var arr = [];
var obj = {};

for(var i=0;i<vs.size();i++){
var label = vs.get(i).getLabel(); 
var value = vs.get(i).getDisplayValue();
obj[label] = value.toString();
}

arr.push(obj);

jsonObj.Variables = arr;

gs.info(JSON.stringify(jsonObj));

}

sample output when I tested in my case; the exact format you wanted

*** Script: {"Variables":[{"Cost Center":"Customer Support","Project":"ITIL Project","Test User":"Abel Tuter1","Specify the time when you want the token link to be initially active":"2019-10-23 10:48:21","Hours":"1","Minutes":"0","Select the policy to be attached to the token":"Super Admin Policy","Specify reason for requesting a token":"New User Login"}]}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

14 REPLIES 14

Hi,

So you don't want variables which don't have any value such as Label, Container Start etc

so update code as below; what it will do is push only those variables and values which are having some value;

variable of type label won't have value so it will be blocked

var value = vs.get(i).getDisplayValue();

if(value!=''){
obj[label] = value.toString();
}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Thanks so much for your response. Your solution worked for me. 

But when I run the same script for Incident table which also contains variable as Request Items, it does not return me any result.

 

For us we have some record producers which creates Incident and those Incident records at the backend have variables recorded in the variable editor in the same way as Requested Item or catalog Task has.

 

How can we extract those variables in the similar format as you mentioned in the response.

 

I tried with the same code but the object "vs" in your code return me 0. 

var gr = new GlideRecord('incident');
gr.addEncodedQuery('sys_id=a9c3b911db7c801835dc403c3a9619a');
gr.query();
if(gr.next()){

var jsonObj = {};

//var ritmSysId = gr.request_item;
gs.info('Incident Sys ID is is' + gr.sys_id);
var set = new GlideappVariablePoolQuestionSet();
set.setRequestID(gr.sys_id);
set.load();
var vs = set.getFlatQuestions();
gs.info('Set Load is' + vs);
gs.info('Set Load is' + vs.size());
var arr = [];
var obj = {};

for(var i=0;i<vs.size();i++){
var label = vs.get(i).getLabel(); 
var value = vs.get(i).getDisplayValue();
obj[label] = value.toString();
}

arr.push(obj);

jsonObj.Variables = arr;

gs.info(JSON.stringify(jsonObj));

}

 

Kindly assist

 

 

Hi,

you won't get the variables for the incident submitted from record producer in that way; that is only used for RITM

use below script to get the variable values from incident

var jsonObj = {};

var arr = [];
var obj = {};

var incRec = new GlideRecord('question_answer');
incRec.addQuery('table_sys_id','a9c3b911db7c801835dc403c3a9619a');
incRec.query();
while (incRec.next()) {

var variableValue = incRec.value;

var variableLabel = incRec.question.getDisplayValue();

gs.print('Variable is: ' + variableLabel + ' value is: ' + variableValue);

obj[variableLabel] = variableValue.toString();

}

arr.push(obj);

jsonObj.Variables = arr;

gs.info(JSON.stringify(jsonObj));

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

Thanks again for sharing and helping. I tried your code as mentioned above, but the issue which I am facing is with Reference fields, it gives me the Sys ID in JSON generated instead of Display Value like for example as mentioned below:

{"Variables":[{"User":"72cd32314fff3e40f1920adf0310c7","Field1":"1","Field2":"2","Field3":"3"}]}

 

Is there a way we can get the Display Value of the Reference fields?

 

Kindly assist!

Hi Ankur,

 

It works like charm!!!! 

This fetches all the variables except the multi row variable set. Is there any way by which we could get the MRVS too..?

 

Thanks,

Amritha