Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get record producer variable (answered questions label and answer) into description

attanhes
Tera Guru

Hi,

I have a record producer with 20 multiple-choice variable questions. I only need answered questions by concatenating answered with questions label and answered into the descriptions field only for those multiple-choice questions.

 

find_real_file.png

I tried the below script in record producer script to get only answered questions label and values but unfortunately, it is not working.

var gr = new GlideRecord('question_answer');  
    //gr.addQuery('table_sys_id', current.sys_id); //sys_id of record in question  5312acc4dbab34102721455215961993
 gr.addQuery('table_sys_id', '85e5f5dcdbabf050691a7b53f39619af'); //sys_id of record in question  5312acc4dbab34102721455215961993 incident
gs.log("Current SysID " + current.getUniqueValue());
    gr.query();  
    var comments = '';
    while (gr.next()) {  
      if (gr.value && gr.question.type == 3) { //exclude questions with empty values  
		//if (gr.value) { //exclude questions with empty values  
      comments += gr.question.getDisplayValue() + ":   " + gr.value.getDisplayValue() + "\n"; //display question and answer  
      }  
    }
    current.comments = comments;
current.description = comments;

Can someone please help me to work this out?

1 ACCEPTED SOLUTION

attanhes
Tera Guru

Hi,

I just worked this out by creating a new before insert business rule in the incident table by adding above script.

find_real_file.png

 

 

View solution in original post

7 REPLIES 7

Hi Alan,

In which table I should write this before insert business rule? This record producer creates incident records.

 

attanhes
Tera Guru

Hi,

I just worked this out by creating a new before insert business rule in the incident table by adding above script.

find_real_file.png

 

 

attanhes
Tera Guru

Hi,

I have further developed this script by adding more functions. Just adding my script into this thread in case it might be helpful for others.

 

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord('question_answer');
    gr.addQuery('table_sys_id', current.sys_id);
    //gr.addQuery('table_sys_id', '85e5f5dcdbabf050691a7b53f39619af'); //sys_id of record in question  5312acc4dbab34102721455215961993 incident
    //gs.log("Current SysID " + current.getUniqueValue());
    gr.query();
    var choice = '';
    var descript = '';
	var category = '';
    while (gr.next()) {
        //if (gr.value){//exclude questions with empty values  
       if (gr.value && gr.question.type == 3 && gr.question.name != "v_facilities_incident_category" )
		//if (gr.value && gr.question.type != 6 && gr.question.type != 8 ) {
		{  
            choice += gr.question.getDisplayValue() + " : " + "\n" + gr.value.getDisplayValue() + "\n"; //display question and answer  
        } 
			else if (gr.question.type == 2) 
			{
				descript += gr.value.getDisplayValue() + "\n";
			} 
			else if (gr.question.name == "v_facilities_incident_category") 
			{
				category += gr.question.getDisplayValue() + " : "+ gr.value.getDisplayValue() + "\n";
			}
    }

    current.description = "============== Category Type ============== " + "\n"+ category 
		+ "\n" + "============== Choices ==============" + "\n" + choice
		+ "\n" + " ============== Description ==============" + "\n" + descript;
    //current.comments = descript;
	current.work_notes = "============== Category Type ============== " + "\n"+ category 
		+ "\n" + "============== Choices ==============" + "\n" + choice
		+ "\n" + " ============== Description ==============" + "\n" + descript;




})(current, previous);

 

This script helps to print the details into description as in below format

find_real_file.png