- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2021 11:52 PM
Requirement : I want to add the record producers variables to custom table(business case).
Approach : 1.Created a BR on insert -request
2. getting variables values from Question answer tables
Question: How to map name: value pair with custom tables fields ?
Below script: i have written to get variables values
var jsonObj = {};
var arr = [];
var obj = {};
var incRec = new GlideRecord('question_answer');
incRec.addQuery('table_sys_id', current.sys_id);
incRec.query();
while (incRec.next()) {
var variableValue = incRec.value;
var variableLabel = incRec.question.getDisplayValue();
obj[variableLabel] = variableValue.toString();
}
arr.push(obj);
jsonObj.Variables = arr;
gs.info(JSON.stringify(jsonObj));
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 12:50 AM
No need to query the question_answer table
You can do it directly like this
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var arr = [];
var variables = current.variables.getElements();
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if(label != '' && value != ''){
var obj = {};
obj[label] = value.toString();
arr.push(obj);
}
}
gs.info(JSON.stringify(arr));
})(current, previous);
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-09-2021 04:04 AM
Hi,
you are referring to record producer then it won't have any Request number
Can you explain your business use-case
I have already shared script on how to generate the JSON array of objects for the variables
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-09-2021 09:49 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 12:50 AM
No need to query the question_answer table
You can do it directly like this
(function executeRule(current, previous /*null when async*/) {
// Add your code here
var arr = [];
var variables = current.variables.getElements();
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
var label = question.getLabel();
var value = question.getDisplayValue();
if(label != '' && value != ''){
var obj = {};
obj[label] = value.toString();
arr.push(obj);
}
}
gs.info(JSON.stringify(arr));
})(current, previous);
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
01-31-2023 09:59 AM
Hello Ankur,
Thank you for the code. It was indeed helpful. I want to ask one additional request in this if you could help me. If I want to make each Label and Value in new line then how can I do it?
Regards
Anirban
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-31-2023 07:06 PM
you can join the array with new line i.e. \n while setting the value in some field
but remember that field should support new line character
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader