I want to map Question- Answer tables value with custom table fields

Supriya Waghmod
Kilo Contributor

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));

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Supriya Waghmode 

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

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

View solution in original post

9 REPLIES 9

Sai Kumar B
Mega Sage
Mega Sage

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

 

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

@Supriya Waghmode 

Did you get a chance to check the script I shared?

Regards
Ankur

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

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