Copy record producer variable information to the change form field

Johny1
Tera Expert

I have created a record producer with variables on the change request table. I need to copy all variables at a time to the created change record field.

I'm using the below script but the output looks is not in the correct format.

var sInputs = 'User Inputs:';
for (var key in producer)
	if (producer[key]){
			sInputs += '\n' + key + ':' + producer[key].getDisplayValue();
}

 

Output:

find_real_file.png

3 REPLIES 3

Kalaiarasan Pus
Giga Sage

You have not stated what is the issue and how are you assigning the information back to the change field.

Sorry. The issue is it shows the sys ids of the variables but I fixed it with the updated script as below. And I'm setting it to the description field via script field in the record producer.

var test = [];
for(var v in producer){ 
   if (v.startsWith("IO")) { //only variables
       var question = new GlideRecord('item_option_new'); 
       question.get(v.substring(2)); // Querying by sys_id to determine question text
       test += question.question_text + ": " + producer[v] + "\n"; // Set key:value pair to variable
   }
}
current.description = test; 

Is there a possibility to display only the variable data excluding variable sets variables? And shall this script can be used globally instead of writing on each record producer?

Please update?

Thanks

Johny

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);