catalog item variable reference field values not displaying the display value in email

Ravivarman Saee
Tera Contributor

i am trying to display the catalog item variable and its values in the email body using an email script. however reference field values not displaying the display value in email and instead only the sys id is filled. how to solve this ?

    var ritm = new GlideRecord('sc_req_item');
    if (current.sysapproval.sys_class_name == 'sc_req_item') {
        ritm.get(current.sysapproval.sys_id);
    }

    if (ritm.isValidRecord()) {
        template.print('<h3>Catalog Item Variables</h3>');
        template.print('<table border="1" cellpadding="5" cellspacing="0">');
        template.print('<tr><th>Variable</th><th>Value</th></tr>');

        var varMtom = new GlideRecord('sc_item_option_mtom');
        varMtom.addQuery('request_item', ritm.sys_id);
        varMtom.query();

        var hasVariables = false;

        while (varMtom.next()) {
            var varOption = varMtom.sc_item_option.getRefRecord();
            if (!varOption) continue;

            var rawValue = varOption.getValue('value');
            if (!rawValue) continue; // skip empty

            var varDef = varOption.getElement('item_option_new').getRefRecord();
            if (!varDef) continue;

            var varLabel = varDef.getValue('question_text') || varDef.getValue('label') || 'Label not found';
            var displayValue = rawValue;

            // Force type to integer
            var varType = parseInt(varDef.getValue('type'), 10);

            // Resolve reference field values properly
            if (varType === 7 && varDef.getValue('reference')) { // Type 7 = reference
                var refGR = new GlideRecord(varDef.getValue('reference'));
                if (refGR.get(rawValue)) {
                    displayValue = refGR.getDisplayValue();
                }
            }

            hasVariables = true;

            template.print('<tr><td><strong>' + varLabel + '</strong></td><td>' + displayValue + '</td></tr>');
        }

        template.print('</table>');

        if (!hasVariables) {
            template.print('<p>No variables provided or all were empty.</p>');
        }
    } else {
        template.print('<p>Requested Item not found.</p>');
    }
4 REPLIES 4

J Siva
Tera Sage

Hi @Ravivarman Saee 
Reference type variable value is 8 not 7.
Modify that and try again.
Regards,
Siva

Ankur Bawiskar
Tera Patron
Tera Patron

@Ravivarman Saee 

script looks fine to me with 1 change, query with value 8 instead of 7 for Reference type variable

Did you add logs and see if table name came fine which you are using in GlideRecord?

the table being referred has any field marked as Display=True at dictionary level?

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ravivarman Saee 

is your script working fine now?

If yes then please close the thread by marking appropriate response as correct.

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