- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 04:26 AM
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!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 05:08 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-11-2020 03:17 AM
Hi Amritha,
for mrvs you will have to check in this table
sc_multi_row_question_answer
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
02-12-2022 06:57 PM
Hi
I am able to get all variables from requested item record excepts for Multi row variables. How can I include Multi row variables as well in same JSON? Please let me know.
Thanks,
Sindhu.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-13-2022 07:39 PM
MRVS holds json structure.
Data for that is present in sc_multi_row_question_answer table.
you can get it from there and use.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2022 07:23 PM
Thank you.. I tried querying 'sc_multi_row_question_answer' table, it works but I am seeing two issues here.
1) It only displays 1 row variables instead of multi row
2) It displays sys_id for reference fields instead of Display value
Is there any way we can achieve this.. Please refer the code below
var mrv = new GlideRecord('sc_multi_row_question_answer');
mrv.addQuery('parent_id',ritmsys_id);
mrv.query();
while (mrv.next()) {
var variableValue = mrv.value();
var variableLabel = mrv.item_option_new.question_text;
obj[variableLabel] = variableValue.toString();
}
arr.push(obj);
jsonObj.Variables = arr;
gs.print(JSON.stringify(jsonObj));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 05:18 AM
Hi There , You dont have to write any lengthy code for this , its pretty simple check out below:
var jsonstring={"Variables":[{"lable1":"ajay1","lable2":"ajay2","lable3":"ajay3"}]};
gs.log(jsonstring.Variables[0].lable1);
gs.log(jsonstring.Variables[0].lable2);
gs.log(jsonstring.Variables[0].lable3);
Output as below:
If this resolves your query, please mark my comments as correct and helpful
.
Regards,
Ajay Chavan