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

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

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

@Supriya Waghmode In Record producer post insert script you send number via current.number(current table request number), you have to use post insert script because number will be generated in target table upon insertion of the record.

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

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

@anirban300 

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

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