How to get value of field in email script?

Buddy 1
Tera Contributor

I want to display a field in email notification.But it is showing the field without any value even though the field is not empty.

How to get the value of field and dispaly the field along with its value in email notification?

Thanks in advance!

10 REPLIES 10

Sagar Pagar
Tera Patron

Hi,

You can add field to the notifications using variables option in notification. select the variable, it will be added to notification body.

Refer below image for your reference -

find_real_file.png

 

OR

You can add fields using email scripts. Create email scripts using current or gliderecord object you can print the variable values.

 

String type - template.print("field label: " + current.getValue("field_name"));

Reference type - template.print("field label: " + current.getDisplayValue("field_name"));

 

Thanks,

Sagar Pagar

 

The world works with ServiceNow

Hi @Sagar Pagar ,

I am trying to achieve with this email script. It is showing the field enquiry description , but its value is not populating. Can you help me and rectify this. Thanks

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {

    // Add your code here

    var emailUtils = new global.QREmailUtils(this);
    var sourceTableDV = current.getClassDisplayValue();
    var language = current.contact.preferred_language;
    var number = current.number + '';
    emailUtils.setLanguage(language);
    emailUtils.addHeader('Case');
    emailUtils.setSubject('ResolvedSubject', [number]);
    emailUtils.addTitle('ResolvedTitle');
    if (current.variables.enquiry_description) {
        emailUtils.addFields(current, ['number', 'order_number', 'short_description', 'enquiry_description']);

    } else {
        emailUtils.addFields(current, ['number', 'order_number', 'short_description']); 
    }    emailUtils.addJournalField(current, 'comments', false);
    emailUtils.addFooter('Thankyou');
    template.print(emailUtils.getContent());


})(current, template, email, email_action, event);

Hi,

You have to check the Script include - QREmailUtils with function - addFields()

In which how it takes the fields values for 'number', 'order_number', 'short_description' fields.

Similarly you have to do for enquiry_description field, then It will print the required values.

 

Also, check the function - getContent()

How it prints the other field values.

 

Thanks,
Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar ,

Thanks for the response!

This is the addField function, and i was still not able to get the value ,Do i miss something here.

addFields: function (record, fields, noBreakLine) {
        var self = this;
        var fieldsContent = '';

        if (fields) {
            fieldsContent = '<table class="table-2-col" border="0" cellpadding="0" cellspacing="10">';
            fields.forEach(function (fieldName) {
                fieldsContent += '<tr>'
                                    + '<td>'
                                        + global.QBhelper.getTranslatedLabel(record, fieldName, self.language) + ':'
                                    + '</td>'
                                    + '<td>'
                                        + global.QBhelper.getDisplayFieldValue(record, fieldName, self.language)
                                    + '</td>'
                                + '</tr>';
            });

            fieldsContent += '</table>';
        }

        if (fields.length > 0)
            this._pushToContent(fieldsContent, 'fields');
        if (fields && fields.length > 0 && !noBreakLine)
            this.addBreak();
    },

*****************************************************************

function getTranslatedLabel(record, field, lang) {
        var dwField = QBhelper.getDotWalkField(record, field);
        var tableHierarchy = j2js(new TableUtils(dwField.tableName).getTables());
        var elementGR = new GlideRecord("sys_documentation");
        var name = elementGR.addQuery("name", 'IN', tableHierarchy.join());
        name.addOrCondition("name", record.sys_class_name);
        elementGR.addQuery("element", dwField.fieldName);
        elementGR.addQuery("language", lang);
        elementGR.query();
        elementGR.setLimit(1);

        if (elementGR.next())
            return elementGR.label + '';

        return dwField.record[dwField.fieldName].getLabel();
    }

function getDisplayFieldValue(record, field, lang) {
        if (gs.nil(field))
            return '';

        if (!lang)
            lang = 'en';

        var dwField = getDotWalkField(record, field);
        var tableName = dwField.tableName;
        var fieldType = dwField.fieldType;
        field = dwField.field;

        switch (fieldType) {
            case 'currency':
            case 'price':
                dv = (function () {
                    var splitCurrStr = field.getCurrencyString().split(';');
                    var grCurrency = new GlideRecord('fx_currency');
                    grCurrency.addQuery('code', splitCurrStr[0]);
                    grCurrency.setLimit(1);
                    grCurrency.query();
                    if (grCurrency.next())
                        return splitCurrStr[1] + " " + grCurrency.getValue('symbol');
                    return field.getDisplayValue();
                }());
                break;
            case 'choice':
            case 'integer':
            case 'string':
                dv = (function () {
                    var choiceGR = new GlideRecord('sys_choice');
                    choiceGR.addQuery('name', 'IN', j2js(new TableUtils(tableName).getTables()).join());
                    choiceGR.addQuery('value', field.toString());
                    choiceGR.addQuery('element', field.getName());
                    choiceGR.addQuery('language', lang);
                    choiceGR.setLimit(1);
                    choiceGR.query();

                    if (choiceGR.next())
                        return choiceGR.getValue('label');
                    return escapeHTML(field.getDisplayValue());
                }());
                break;
            default:
                dv = escapeHTML(field.getDisplayValue());
        }

        return dv;
    }