How can we change the background color of the row of an email template from script based on conditio

PuluM
Tera Expert

Hi Everyone,
I have configured the email template where I have created table with the subject and all other required fields. Now I want to alter the background color row which contains subject based on the task type of communication .
I have an email script to populate the subject. Now I want to have custom background color to the emails based on the communication task type.
Can you please help me with the script.
I have tried with below code in email script.

ans= "<div style='background-color:" + color + ";border:1px ;padding: 0px !important;'>" + result + "<div>";
            template.print(ans);
But this is not working fully. In sow I can see color overlap not able to control that behavior.
mukeshp_0-1746168264192.png

This is how it looks I only want the green as a background for the condition. Based on the condition its changing  background but still i see the different color in the border. I want to get rid of that color. How can we achieve this.

Source Code:
</style>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table style="font-family: 'Frutiger 45 Light', Arial, Helvetica, sans-serif; color: rgb(34, 34, 34); font-size: 10.5pt; border-collapse: collapse; border: 0px transparent; width: 100%; height: 368.563px;"><colgroup><col style="width: 21.6113%;"><col style="width: 78.3887%;"></colgroup>
<tbody>
<tr style="height: 27.0625px; background-color: rgb(241, 196, 15);">
<td style="text-align: center; border: 1px solid rgb(169, 169, 169); font-weight: bold; font-size: 14.5pt; padding: 5px; height: 27.0625px;" colspan="2">
<div style="background-color: rgb(45, 194, 107); border: 1px; padding: 0px !important;"><br>
<div>Test Result</div>
</div>
</td>
</tr>

Source code on email template which is configured:
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<table style="font-family: 'Frutiger 45 Light', Arial, Helvetica, sans-serif; color: rgb(34, 34, 34); font-size: 10.5pt; border-collapse: collapse; border: 0px transparent; width: 100%; height: 381.235px;"><colgroup><col style="width: 21.6113%;"><col style="width: 78.3887%;"></colgroup>
<tbody>
<tr style="height: 20.1406px;">
<td style="text-align: center; border: 1px solid rgb(169, 169, 169); font-weight: bold; font-size: 14.5pt; padding: 5px; height: 27.0625px;" colspan="2">Test result</td>
</tr>
<tr style="height: 19.5938px;">
<td class="label" style="height: 19.5938px;" colspan="2">Incident Description</td>
</tr>

Can you please help with script changes which will make it work @Ankur Bawiskar 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@PuluM 

you should write email script for that so that you can show row color based on value of field

Simply using HTML won't help here

Did you start with the email scripting? if yes then share that here

something like this in email script

(function runMailScript(current, template, email, email_action, event) {

    // Add your code here

    // Function to determine the background color based on the task type
    function getBackgroundColor(taskType) {
        var color;
        switch (taskType) {
            case 'Type1':
                color = 'rgb(45, 194, 107)'; // Green
                break;
            case 'Type2':
                color = 'rgb(241, 196, 15)'; // Yellow
                break;
                // Add more cases as needed
            default:
                color = 'rgb(255, 255, 255)'; // Default to white
        }
        return color;
    }

    // Example task type
    var taskType = current.taskTypeField.toString(); // Replace with actual task type
    var color = getBackgroundColor(taskType);

    var result = "Test Result"; // Replace with actual result content
    var ans = "<div style='background-color:" + color + ";border:0px;padding:0px !important;' class='custom-background'>" + result + "</div>";
    template.print(ans);

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

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

(function runMailScript(current, template, email, email_action, event) {
    //Generate the subject line using the helper script include
    var result = new IM_Utilities().EmailSubject(current);
    var incType = current.incident_alert.comm_plan_type.getDisplayValue();
    var taskType = current.comm_task_type.getDisplayValue();
    var subject = '';
    var bgc = '#ffffff';
    if (incType == 'Major Incident') {
        if (taskType == 'Updates') {
            bgC = "rgb(241, 196, 15)"; // orange
            subject += "<div style='background-color:" + bgC + ";border:1px ;padding: 0px !important;'>" + result + "<div>";
            template.print(subject);
        } else if (taskType == 'Closure') {
            bgC = "rgb(45, 194, 107)"; // green color
            subject += "<div style='background-color:" + bgC + ";border:1px ;padding: 0px !important;'>" + result + "<div>";
            template.print(subject);
        } else
            template.print(result);/// major initial communication

    } else
        template.print(result);

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

The above is the script which I have added in mail script

Hi @Ankur Bawiskar  is something wrong with my code? Do I need to change anything please suggest.

 

@PuluM 

try this

1) you should use correct variable name

2) you should close your div with </div> and not with <div>

(function runMailScript(current, template, email, email_action, event) {
    var result = new IM_Utilities().EmailSubject(current);
    var incType = current.incident_alert.comm_plan_type.getDisplayValue();
    var taskType = current.comm_task_type.getDisplayValue();
    var subject = '';
    var bgColor = '#ffffff'; // default

    if (incType == 'Major Incident') {
        if (taskType == 'Updates') {
            bgColor = "rgb(241, 196, 15)"; // orange
        } else if (taskType == 'Closure') {
            bgColor = "rgb(45, 194, 107)"; // green
        }
        subject = "<div style='background-color:" + bgColor + ";border:1px solid #ccc;padding:8px 12px;'>" + result + "</div>";
        template.print(subject);
    } else {
        template.print(result);
    }
})(current, template, email, email_action, event);

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