Script to pull the record producer variables data into Case Description

Chandana E S
Tera Contributor

Hi,

We have requirement where in there are 30+ variables added to case form using the record producer. These variables are hidden on the service portal and are visible to the user on the native UI after the case is created from the service portal. Once the next user updates the data on the variables displayed on the native case UI, this variables data should be pulled into the Case Description.

I have searched on the community and could find few scripts. I have slightly modified it for my requirement and placed it under the script section of the Record producer. The description pulls all the questions but the responses are always null even when the variables have some answer data. 

With further research, I understood that there should be a business rule to copy those variables data and there should be an onLoad client script for those copied variables to pull into the case description. Can you please guide me with the script as I am still learning with these kind of requirements?

Any help with the script is much appreciated. Thank you!

Here is the script that I had placed under the script section of the Record producer:

var arr_questionSysIds = [];
for (var v in producer) {
    if (v.startsWith("IO")) { //only variables
        arr_questionSysIds.push(v.substring(2));
    }
}

var gr_questions = new GlideRecord('item_option_new');
gr_questions.addQuery('sys_id', 'IN', arr_questionSysIds.toString()); //Single call to the table with all variables we are looking for
gr_questions.orderBy('order'); //Order by the order they are on the catalog item form
gr_questions.query();
var description = "";
while (gr_questions.next()) {
    var question = gr_questions.getValue('question_text');
if (question!=null) //ignores container end and line breaks
{
var gr_answer = new GlideRecord('question_answer');
gr_answer.addQuery('table_name', current.getTableName);
gr_answer.addQuery('table_sys_id', current.sys_id);
gr_answer.query();
var value = gr_answer.value;
description += question + ": " + value + "\n";     
//var value = producer[gr_questions.getValue('name')].getDisplayValue();
/*  
  if (value !== "") { //only get variables with values
        description += question + ": " + value + "\n"; 
    }
*/
}
}

current.description = description;
8 REPLIES 8

Hi Mahendra,

Thanks for your response.

I tried creating a display business rule with the above script that you had shared but no luck yet. 

Could you please share your latest BR script?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

can you share your latest BR script with screenshots

regards
Ankur

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

Skander
Tera Contributor

I am a little late to the party but here is a Business Rule (Before) we created for a client to copy all the variables to the description for Record Producers. It is not complete. Depending on the field types that you need other than these: Reference, List collector, Select Box , Single and Multiple Line Text. Also it takes care of empty variables.

 

 

(function executeRule(current, previous /*null when async*/ ) {
	
	var type, name, value, reference, val, item;
    var producerVars = new GlideRecord('question_answer');
    producerVars.addQuery('table_sys_id', current.sys_id);
    //Exclude Label and Container variables
    producerVars.addQuery('question.type', '!=', 11);
    producerVars.addQuery('question.type', '!=', 19);
    producerVars.addQuery('question.type', '!=', 20);
    // Set Order
    producerVars.orderBy('order');
    producerVars.query();
    var descriptionBuilder = '';
    while (producerVars.next()) {
        if (producerVars.value) {
            //Add variable names and values to the description
            type = producerVars.question.type.toString();
            name = producerVars.question.question_text.toString();
            val = producerVars.value.toString();
            // Reference
            if (type == 8 ) {
                reference = producerVars.question.reference.toString();
                value = getReferenceDisplayValue(reference, val);
                descriptionBuilder += (name + ":   " + value + "\n");
            }
            // Select Box
            else if (type == 5) {
                item = producerVars.question.sys_id;
                value = getChoiceDisplayValue(item, val);
                descriptionBuilder += (name + ":   " + value + "\n");
            }
            // List Collector
            else if (type == 21) {
                var valSplit = val.split(',');
				var arr = [];
                for (i = 0; i < valSplit.length; i++) {
                    value = getChoiceDisplayValueList(valSplit[i]);
					arr.push(value);
					arr.join(',');
                }
				descriptionBuilder += (name + ":   " + arr + "\n");
            }
            // Default
            else {
                value = val;
                descriptionBuilder += (name + ":   " + val + "\n");
            }

            // Replace blank values with
            if (value == '') {
                value = '';
            }
        }
    }
    current.description = descriptionBuilder;
    // Function to convert reference value to display value
    function getReferenceDisplayValue(table, value) {
        var displayValue1 = '';
        var gr1 = new GlideRecord(table);
        gr1.addQuery('sys_id', value);
        gr1.query();
        if (gr1.next()) {
            displayValue1 = gr1.getDisplayValue();
        }
        return displayValue1;
    }
    // Function to convert choice value to display value
    function getChoiceDisplayValue(item_option, value) {
        var displayValue2 = '';
        var gr2 = new GlideRecord('question_choice');
        gr2.addEncodedQuery('question=' + item_option + '^value=' + value);
        gr2.query();
        if (gr2.next()) {
            displayValue2 = gr2.text + '';
        }
        return displayValue2;
    }
	// Function to convert choice value to display value for List Collector
    function getChoiceDisplayValueList(value) {
        var displayValue2 = '';
        var gr2 = new GlideRecord('question_choice');
        gr2.addEncodedQuery('sys_id=' + value);
        gr2.query();
        if (gr2.next()) {
            displayValue2 = gr2.text + '';
        }
        return displayValue2;
    }
})(current, previous);