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.

Pass Record Producer variables questions and answers to Description field

Dazler
Mega Sage

Hi,

I have created a Record Producer form for our incident table.  They don't want the variable editor on the Incident form, so instead to retrieve the questions and answers I suggested we pass them into the description field.

I found this script and it sort of works.

var populatedList = '';
 
for (v in current.variables) { 
var question = current.variables[v].getQuestion(); 

if(question.getLabel() != '');
{
populatedList += question.getLabel() + ":" + question.getDisplayValue() + '\n';
}

}

 

But it is retrieving all the variables questions and answers, including those that are not answered and null fields.

find_real_file.png

Does anyone have a better script for this?

Any help would be appreciated.

8 REPLIES 8

sachin_namjoshi
Kilo Patron
Kilo Patron

You can use record producer script like below to pass particular variables to incident description.

 

current.description = producer.comments + producer.name; // comments and name are the name of record producer variables

Regards,

Sachin

Hi @sachin.namjoshi 

I am not trying to pass values individual.  I want to pull all the questions and answers that were filled out.  Kind of like I want to pull the variable summary, which will only show me the questions that were filled out.

Record producer variable values are stored in question_answer table.

Your record producer script can query question_answer to get questions and answers.

 

You can use sample script like below to get all RP variable values.

 

var jsonObj = {};
var arr = [];
var obj = {};
var incRec = new GlideRecord('question_answer');
incRec.addQuery('table_sys_id', '7d28cea5dbf00010b94b58b3ca9619b9');
incRec.query();
while (incRec.next()) {
    var variableValue = incRec.value;
    var variableLabel = incRec.question.getDisplayValue();

    //check to see if 'value' is the sys_id
    if (variableValue.toString().length == 32) {
        var ciGR = new GlideRecord('cmdb_ci');
        ciGR.get(variableValue);
        ciGR.query();
        if (ciGR.next()) {
            //now you can retrieve any value related to sys_id
            variableValue = ciGR.name;
        }
    }

    gs.print('Question is: ' + variableLabel + ' || Value is: ' + variableValue);
    obj[variableLabel] = variableValue.toString();
}
arr.push(obj);
jsonObj.Variables = arr;
gs.info(JSON.stringify(jsonObj));

 

Regards,

Sachin

Hi @sachin.namjoshi 

This script works, but with the some of my values are reference fields and are display at the sys_id.

 

Am I able to retrieve the display name or am I going to have to query the table to retrieve the display name of that sys_id?