Variables in the notification by Script include

jkelvynsant
Tera Contributor

Hello, I'm trying to create notifications using the include script that send all the variables from the request or incident to the requester. In other words, every time a request is created, an email is sent to the responsible party with all the variables that have been filled in. Sending the RITM variables is working, but the incident notification is returning the values ​​of the variables. Could someone help me figure out where the problem is?

var variablesHtml = this.getRecordVariables(record);
                if (variablesHtml) {
                    html.push(variablesHtml);
                    html.push('<br>');
                }

======================

getRecordVariables: function(record) {
            var varHtml = [];
            var tableName = record.getTableName();
            var recordSysId = record.getUniqueValue();

            try {
                if (tableName === 'sc_req_item') {
                    varHtml = this._getVariablesFromQuestionSet(recordSysId);
                    return varHtml.join('');
                }
               
                else {
                    varHtml = this._getVariablesFromQuestionAnswer(tableName, recordSysId);
                    return varHtml.join('');
                }
            } catch (error) {
                gs.error('acelenNotificationUtils.getRecordVariables() Error: ' + error);
                return '';
            }
        },

=========================================================

 _getVariablesFromQuestionAnswer: function(tableName, recordSysId) {
            var varHtml = [];
            try {
                var qaRecord = new GlideRecord('question_answer');
                qaRecord.addQuery('table_name', tableName);
                qaRecord.addQuery('table_sys_id', recordSysId);
                qaRecord.addQuery('question.type', '!=', 'multi_row_variable_set');
                qaRecord.orderBy('order');
                qaRecord.query();

                while (qaRecord.next()) {

                    var label =
                        qaRecord.question.getDisplayValue('label') || qaRecord.question.getDisplayValue('question_text');
                    var value = qaRecord.value.getDisplayValue();
                    if (label && value && value !== 'false' && value !== '' && value !== 'null') {
                        varHtml.push('<b>' + label + ':</b> ' + value + '<br>');
                    }
                }
            } catch (error) {

                gs.error('acelenNotificationUtils._getVariablesFromQuestionAnswer() Error: ' + error);
            }
            return varHtml;
        },
1 REPLY 1

Deepak Shaerma
Mega Sage
Mega Sage

Hi @jkelvynsant 

as i see the issue lies in this line: var value = qaRecord.value.getDisplayValue();

use targetRecord.variables[variableID].getDisplayValue() to fetch the data.

_getVariablesFromQuestionAnswer: function(tableName, recordSysId) {
    var varHtml = [];
    
    // 1. Get the target record (e.g., the Incident) so we can access its variable pool
    var targetRecord = new GlideRecord(tableName);
    if (!targetRecord.get(recordSysId)) {
        return ''; // Return empty if record not found
    }

    try {
        var qaRecord = new GlideRecord('question_answer');
        qaRecord.addQuery('table_name', tableName);
        qaRecord.addQuery('table_sys_id', recordSysId);
        qaRecord.addQuery('question.type', '!=', 'multi_row_variable_set');
        qaRecord.orderBy('order');
        qaRecord.query();

        while (qaRecord.next()) {
            var label = qaRecord.question.getDisplayValue('label') || qaRecord.question.getDisplayValue('question_text');
            
            // 2. Get the Variable ID
            var variableId = qaRecord.question.toString(); 

            // 3. Use the target record's variable pool to get the true Display Value
            // Accessing .variables by ID is safer than name because names can change or be empty
            var value = targetRecord.variables[variableId].getDisplayValue();

            if (label && value && value !== 'false' && value !== '' && value !== 'null') {
                varHtml.push('<b>' + label + ':</b> ' + value + '<br>');
            }
        }
    } catch (error) {
        gs.error('acelenNotificationUtils._getVariablesFromQuestionAnswer() Error: ' + error);
    }
    return varHtml;
},

 

Happy to help! If this resolved your issue, kindly mark it as the correct answer   and Helpful and close the thread 🔒 so others can benefit too.

Warm Regards,

Deepak Sharma

Community Rising Star 2025