Help with Script: Populating Description Field from Answered Record Producer Questions

Sysop
Tera Contributor

Hello,

 

I've been working on a ServiceNow script designed to extract all answered questions from a record producer and then use this information to populate the description field of the resulting record. The aim is to ensure the description only contains answered questions in a neat, ordered manner.

The script does the following:

 

  1. Iterates through each question in the record producer.
  2. If the question has an answer, the script checks the type of field:
    • For reference fields, it fetches the display value.
    • For choice fields, it attempts to get the label instead of the value.
  3. Each answered question, along with its answer, is stored in an array in the order they appear.
  4. The ordered list of questions and answers is concatenated into a single string.
  5. This string is then set as the description for the current record.

While the script is working to some extent, I'm facing issues with the return values of reference and choice fields. Reference fields are returning sys_ids, and choice fields are providing the numeric value rather than the human-readable label.

 

// Extracting answered questions and order them
var questionsArray = [];
for (var v in producer) {
    if (v.startsWith("IO")) {
        if (producer[v] && producer[v] !== '' && producer[v] !== 'false') {
            var question = new GlideRecord('item_option_new');
            if (question.get(v.substring(2))) {
                var answer = producer[v];

                // For reference fields, get the display value
                if (question.question_type == 'reference') {
                    var refRecord = new GlideRecord(question.reference);
                    if (refRecord.get(answer)) {
                        answer = refRecord.getDisplayValue();
                    }
                }
                
                // For choice fields, get the label instead of value
                else if (question.question_type == 'choice') {
                    var choiceGR = new GlideRecord('sys_choice');
                    choiceGR.addQuery('name', question.table); // Get the table dynamically based on the reference.
                    choiceGR.addQuery('element', question.column_name);
                    choiceGR.addQuery('value', answer);
                    choiceGR.query();
                    if (choiceGR.next()) {
                        answer = choiceGR.label;
                    }
                }

                questionsArray.push({
                    order: question.order,
                    text: question.question_text + ": " + answer
                });
            }
        }
    }
}

questionsArray.sort(function(a, b) {
    return a.order - b.order;
});

var answeredQuestionsDescription = '';
questionsArray.forEach(function(q) {
    answeredQuestionsDescription += q.text + "\n\n";
});

current.description = answeredQuestionsDescription;

 

Any insights or suggestions to rectify these issues would be greatly appreciated!

1 REPLY 1

adiddigi
Tera Guru

This is a very expensive script, doing a bunch of stuff. Ideally, I would recommend writing this as an onSubmit Catalog Client script. 

Is there a reason you've not considered doing it via an onSubmit Catalog Client script?