- 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 12:38 AM
You would like to map those values to another custom table other than the Record producer target table?
If yes then you can use the script section in Record producer, Glide to custom table map your record producer values to that custom table.
Sample Script:
var custom = new GlideRecord('custom_table');
custom.intialize();
custom.field_name1 = producer.variable-name1;
custom.insert();
Please mark my answer as helpful/correct, if applicable.
Best regards,
Sai Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 02:32 AM
1.How can I send the request number through this??
2.multiple values of array - to add one field
if(producer.aua==true){
arr.push("AUA").toStrin();
}
if(producer.eur==true){
arr.push("EUR").toString();
}
if(producer.soa==true){
arr.push("SOA").toString();
}
if(producer.noa==true){
arr.push("NOA").toString();
}
gs.log("TEST-SW"+arr);
need set att custome table field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2021 03:14 AM
Did you get a chance to check the script I shared?
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 03:20 AM
Thanks for reply.
Ankur I am trying another approach- scripting through producer
currently stuck @
1.How can I send the request number through this??
2.multiple values of array - to add one field
if(producer.aua==true){
arr.push("AUA").toString();
}
if(producer.eur==true){
arr.push("EUR").toString();
}
if(producer.soa==true){
arr.push("SOA").toString();
}
if(producer.noa==true){
arr.push("NOA").toString();
}
gs.log("TEST-SW"+arr);
need set att custome table field