How to remove null filed in approval email

Brahmi Pandla
Tera Guru

Hi @everyone,

 

Could you please help me with remove null filed in Approval email, please check below snap

BrahmiPandla_0-1770805645675.png

 

7 REPLIES 7

@Brahmi Pandla 

did you add gs.info() and debug at which iteration it's giving null?

Does your catalog item have MRVS inside it?

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

@Brahmi Pandla 

please use item.variables.getElements() instead of getFlatQuestions()

updated script

template.print("Summary of Requested item:\n");
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.sysapproval);
item.query();
while (item.next()) {
    var nicePrice = item.price ? parseFloat(item.price).toFixed(2) : '';
    template.print(item.number + ": " + item.cat_item.getDisplayValue() + (nicePrice ? " - $" + nicePrice : "") + "\n");
    template.print("    Options:\n");

    template.print("<table style='border-collapse: collapse; border-color: white; background-color: lightblue; width: 400px;' border='1' cellspacing='0' cellpadding='4'>");
    template.print("<thead><tr style='background-color: #4CAF50; color: white;'><th style='padding: 8px;'>Item</th><th style='padding: 8px;'>Value</th></tr></thead>");
    template.print("<tbody>");

    var variables = item.variables.getElements();
    var rowCount = 0;

    for (var i = 0; i < variables.length; i++) {
        var question = variables[i].getQuestion();
        var label = question.getLabel();
        var displayValue = question.getDisplayValue();

        // Skip empty/null/false values or missing labels
        if (!label || !displayValue || displayValue === "false" || displayValue === "") continue;

        // Specific cat_items that always show variables
        var specificCats = [
            'cf2a3f204f0d2a006209ecee0210c7df',
            'aa895f894f4acb0029160ad14210c70d',
            '2f7aafc04f1dab80aca9e28d0210c743'
        ];

        // Specific labels to always show
        var specificLabels = [
            "Contact name (If different from Requested By)",
            "[Optional] Contact phone",
            "Name (needs to be selected from the Service Catalog Home Page)",
            "User ID",
            "Region",
            "Job  Title",
            "New IFF employee? (For rehire or relocation, choose No)",
            "Location",
            "Business Unit",
            "Equivalent user",
            "Requested For Name",
            "Job Title",
            "Requested By Name",
            "Create new AD user",
            "Frutarom File Server permissions",
            "Please provide the File Server path",
            "Frutarom HPM (Hyperion)",
            "Frutarom EIS (Finance Reporting)",
            "Justification for your access request:"
        ];

        var showVar = specificCats.indexOf(item.cat_item.toString()) !== -1 ||
            specificLabels.indexOf(label) !== -1 ||
            current.wf_activity && current.wf_activity.name == 'Get manager approval';

        if (showVar) {
            // Handle multi-value (list collectors) - convert to bullet list if array
            var formattedValue = displayValue;
            if (displayValue.indexOf(',') !== -1) {
                var values = displayValue.split(',').map(function(v) {
                    return v.trim();
                });
                formattedValue = values.join('<br>• ');
            }

            template.print("<tr style='border: 1px solid #ddd;'>");
            template.print("<td style='padding: 8px; font-weight: bold; width: 50%;'>" + label + "</td>");
            template.print("<td style='padding: 8px;'>" + formattedValue + "</td>");
            template.print("</tr>");
            rowCount++;
        }
    }

    if (rowCount === 0) {
        template.print("<tr><td colspan='2' style='padding: 12px; text-align: center; color: #666;'>No variables to display</td></tr>");
    }

    template.print("</tbody></table><br>");
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hi @Brahmi Pandla ,

NOTE: Before Trying below mail script save your old mail script and added comments where required.

Mail Script:

// --- Unchanged ---
template.print("Summary of Requested item:\n");
var item = new GlideRecord("sc_req_item");
item.addQuery("sys_id", current.sysapproval);
item.query();

 

while (item.next()) {
    var nicePrice = item.price.toString();
    if (nicePrice != '') {
        nicePrice = parseFloat(nicePrice);
        nicePrice = nicePrice.toFixed(2);
    }

 

    template.print(item.number + ":  " + item.cat_item.getDisplayValue() + "\n");
    template.print("    Options:\n");

 

    var keys = new Array();
    //var set = new Packages.com.glideapp.servicecatalog.variables.VariablePoolQuestionSet();
    var set = new GlideappVariablePoolQuestionSet();
    set.setRequestID(item.sys_id);
    set.load();
    var vs = set.getFlatQuestions();

 

    template.print("&lt;table style=' border-color: white; background-color: lightblue;' border='1' width='400' cellspacing='2' cellpadding='2'&gt;&lt;tbody&gt;");
    template.print("&lt;tr style=' border-color: white; background-color: lightblue;' border='1' width='400' cellspacing='2' cellpadding='2'&gt;&lt;th&gt;Item&lt;/th&gt;&lt;th&gt;Value&lt;/th&gt;&lt;/tr&gt;");

 

    // -----------------------------
    // BEGIN VARIABLE ROWS LOOP
    // -----------------------------

 

    for (var i = 0; i < vs.size(); i++) {

 

        // ================================
        // ADDED: Defensive variables
        // ================================
        var v = vs.get(i); // <-- ADDED

 

        // ================================
        // CHANGED: Safer label handling
        // (Previously: if (vs.get(i).getLabel() != ''))
        // ================================
        var label = v.getLabel();
        if (!label || label.trim() === '') {
            continue; // skip variables with no label
        }

 

        // ================================
        // ADDED: Normalize and filter display value
        // (This is what hides null/empty/false rows)
        // ================================
        var rawDV = v.getDisplayValue();
        var dv = (rawDV === null || rawDV === undefined) ? '' : String(rawDV).trim();

 

        function isBlank(x) {
            return x === null || x === undefined || String(x).trim() === '';
        }

 

        var dvLower = dv.toLowerCase();
        var drop =
            isBlank(dv) ||
            dvLower === 'null' ||
            dvLower === 'undefined' ||
            dvLower === 'false' ||
            dvLower === 'n/a' ||
            dvLower === 'na';

 

        // Optional hardening: if display value is empty but raw value exists, use raw value
        // ADDED (optional but safe):
        if (drop) {
            var rawVal = v.getValue(); // underlying value
            var hasRealValue = !isBlank(rawVal) && String(rawVal).toLowerCase() !== 'null';
            if (hasRealValue) {
                dv = String(rawVal).trim();
                var dvLower2 = dv.toLowerCase();
                drop = isBlank(dv) ||
                       dvLower2 === 'null' ||
                       dvLower2 === 'undefined' ||
                       dvLower2 === 'false' ||
                       dvLower2 === 'n/a' ||
                       dvLower2 === 'na';
            }
        }

 

        if (drop) {
            continue; // <-- ADDED: do not print this row
        }

 

        // ================================
        // CHANGED: Preserve your allow-list logic,
        // but now applied AFTER null/empty filtering
        // ================================
        var shouldPrint = true;

 

        if (item.cat_item == 'cf2a3f204f0d2a006209ecee0210c7df' ||
            item.cat_item == 'aa895f894f4acb0029160ad14210c70d' ||
            item.cat_item == '2f7aafc04f1dab80aca9e28d0210c743') {

 

            var allowedLabels = {
                "Contact name (If different from Requested By)": 1,
                "[Optional] Contact phone": 1,
                "Name (needs to be selected from the Service Catalog Home Page)": 1,
                "User ID": 1,
                "Region": 1,
                "Job  Title": 1,
                "New IFF employee? (For rehire or relocation, choose No)": 1,
                "Location": 1,
                "Business Unit": 1,
                "Equivalent user": 1,
                "Requested For Name": 1,
                "Job Title": 1,
                "Requested By Name": 1,
                "Create new AD user": 1,
                "Frutarom File Server permissions": 1,
                "Please provide the File Server path": 1,
                "Frutarom HPM (Hyperion)": 1,
                "Frutarom EIS (Finance Reporting)": 1,
                "Justification for your access request:": 1
            };

 

            shouldPrint = allowedLabels[label] ||
                          (current.wf_activity && current.wf_activity.name == 'Get manager approval');
        }

 

        if (!shouldPrint) {
            continue;
        }

 

        // ================================
        // CHANGED: Print sanitized label/value
        // (same structure, just using label/dv variables)
        // ================================
        template.print(
            "&lt;tr style=' border-color: white; background-color: lightblue;' border='1' width='400' cellspacing='2' cellpadding='2'&gt;" +
            "&lt;td&gt;" + label + "&lt;/td&gt;&lt;td&gt;" + dv + "&lt;/td&gt;&lt;/tr&gt;"
        );
    }

 

    template.print("&lt;/tbody&gt;&lt;/table&gt;");
}


Please mark as helpful if this solves the issue.

Thanks
Yamsani Bhavani