We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

in javascript can we use html

Vedavalli
Mega Sage

In notifications I've configured for team actionable notification. SO IN LINKN TO CONTENT I'VE written javascript to show the variables i wanted to show in tabular format. So how to use html in that script . i used table,tr,td tags not working

7 REPLIES 7

@Vedavalli 

I don't think HTML tags work there.

For testing you can try adding a message which is with bold tag <b>

If it works then table tag should also work

if not then HTML tags are not supported there.

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Bhuvan
Giga Patron

Tejas Adhalrao
Tera Guru

Hi @Vedavalli ,

in your script Right now, you are joining the text using ** and \n.
These symbols work only for plain text not for HTML.

can you pls try this script :

(function getNotificationHeadingAndMessage(
    target,
    event,
    recipientTable,
    recipientSysId,
    vaNotificationRuntimeContext
) {
    var approval_record = new GlideRecord("sysapproval_approver");
    approval_record.get(target.sys_id);

    var requested_item_record = approval_record.sysapproval.getRefRecord();
    var ritm_sysid = requested_item_record.sys_id;
    var ritm_number = requested_item_record.number;

    var gr = new GlideRecord("sc_req_item");
    gr.get(ritm_sysid);
    var item = gr.cat_item.name;

    var messageHeading = [ritm_number, item];
    var messageContent = "<p>This requested item needs your review.</p>";

    // Start HTML table
    var variablesDetails = "<table style='border-collapse:collapse; width:100%;' border='1' cellpadding='5'>";
    variablesDetails += "<tr><th>Question</th><th>Answer</th></tr>";

    var variables = gr.variables.getElements();
    for (var i = 0; i < variables.length; i++) {
        var question = variables[i].getQuestion();
        variablesDetails +=
            "<tr><td>" +
            question.getLabel() +
            "</td><td>" +
            question.getDisplayValue() +
            "</td></tr>";
    }

    // Close the table
    variablesDetails += "</table>";

    messageContent += variablesDetails;

    return {
        messageHeading: messageHeading.join(" - "),
        messageContent: messageContent
    };
})(target, event, recipientTable, recipientSysId, vaNotificationRuntimeContext);