- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2019 06:41 AM
Hi,
I need to fetch all the variable information of a record producer which creates an Incident once submitted in the below format of JSON
{"Variables":[{"variable1 Label":"Variable 1 Value","variable2 Label":"Variable 2 Value","variable3 Label":"Variable 3 Value"}]}
I posted a similar query where About helped me in getting the required JSON format using the script below:
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));
But the challenge is with reference fields, it gives me the sys I'd of the reference type variable instead of the Display value.
Can someone please help me on how to achieve this requirement of fetching all variable information of a Record Producer.
Kindly assist!!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2019 01:12 PM
Hey snow.04,
The only reference field on the [question_answer] table is the 'Question' field. The 'Value' field is a String field type so you can't get the display value on the Value field the same way you did for the Question field. You have to use the value of the sys_id and query the [cmdb_ci] table with it:
var jsonObj = {};
var arr = [];
var obj = {};
var incRec = new GlideRecord('question_answer');
incRec.addQuery('table_sys_id', '7d28cea5dbf00010b94b58b3ca9619b9');
incRec.query();
while (incRec.next()) {
var variableValue = incRec.value;
var variableLabel = incRec.question.getDisplayValue();
//check to see if 'value' is the sys_id
if (variableValue.toString().length == 32) {
var ciGR = new GlideRecord('cmdb_ci');
ciGR.get(variableValue);
ciGR.query();
if (ciGR.next()) {
//now you can retrieve any value related to sys_id
variableValue = ciGR.name;
}
}
gs.print('Question is: ' + variableLabel + ' || Value is: ' + variableValue);
obj[variableLabel] = variableValue.toString();
}
arr.push(obj);
jsonObj.Variables = arr;
gs.info(JSON.stringify(jsonObj));
Remember to change the sys_id for the [question_answer] query. This should get you the name of the application from the record producer.
Hope this answer helped or solved your problem. If so, please mark it as such. Thanks!
James Gragston | Developer at CloudPires
5721 Dragon Way | Suite 118
Cincinnati, OH 45227
CloudPires.com

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-29-2019 01:12 PM
Hey snow.04,
The only reference field on the [question_answer] table is the 'Question' field. The 'Value' field is a String field type so you can't get the display value on the Value field the same way you did for the Question field. You have to use the value of the sys_id and query the [cmdb_ci] table with it:
var jsonObj = {};
var arr = [];
var obj = {};
var incRec = new GlideRecord('question_answer');
incRec.addQuery('table_sys_id', '7d28cea5dbf00010b94b58b3ca9619b9');
incRec.query();
while (incRec.next()) {
var variableValue = incRec.value;
var variableLabel = incRec.question.getDisplayValue();
//check to see if 'value' is the sys_id
if (variableValue.toString().length == 32) {
var ciGR = new GlideRecord('cmdb_ci');
ciGR.get(variableValue);
ciGR.query();
if (ciGR.next()) {
//now you can retrieve any value related to sys_id
variableValue = ciGR.name;
}
}
gs.print('Question is: ' + variableLabel + ' || Value is: ' + variableValue);
obj[variableLabel] = variableValue.toString();
}
arr.push(obj);
jsonObj.Variables = arr;
gs.info(JSON.stringify(jsonObj));
Remember to change the sys_id for the [question_answer] query. This should get you the name of the application from the record producer.
Hope this answer helped or solved your problem. If so, please mark it as such. Thanks!
James Gragston | Developer at CloudPires
5721 Dragon Way | Suite 118
Cincinnati, OH 45227
CloudPires.com