Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to change background color based on field value

Hari1
Mega Sage

Hi All,

I have a requirement to change the table <td> tag background color as the field value changes at the time of insertion.

find_real_file.png

When the value of INC field value is "true" i want the <td> tag background colour to be red

When the value of INC field value is "false" i want the <td> tag background colour to be yellow.

Script:

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) 
{
	var baseUrl = gs.getProperty("glide.servlet.uri");
    email.setSubject("Applications - Last created Incidents (Days & Date) Report as on " + gs.now());
	var finalJSONArray =[];
	var obj = {};
	var url,shortDesc,assignGroup,escalated,inc;
	var gr = new GlideRecord('incident');
	gr.addQuery('active','true');
	gr.addQuery('assigned_to','46d44a23a9fe19810012d100cca80666');
	gr.query();
	while(gr.next())
	{
		shortDesc = gr.short_description.getDisplayValue();
		assignGroup = gr.assignment_group.getDisplayValue();
		complex = gr.u_complexity.getDisplayValue();
		inc = gr.u_inc.getDisplayValue();
		url = '<a href="' + baseUrl + 'sp?id=ticket&table=' + gr.getTableName() + '&sys_id=' + gr.sys_id + '">' + gr.number + '</a>';
		gs.log("url: " + url);
		
		obj = {URL : url ,ShortDesc : shortDesc, AssignmentGroup : assignGroup,Complexity: complex, IncGreq : inc};
		finalJSONArray.push(obj);
		}
	gs.info("obj is" + JSON.stringify(obj));
	gs.info("finalJSONArray is" + JSON.stringify(finalJSONArray));
	
	template.print("<p><table border=3>");
    template.print("<tr bgcolor=yellow><td colspan=3 align =center><b>Incident details</b></td></tr>");
    template.print("<tr><th>Number</th><th>Short Desc</th><th>Assignment Group</th><th>INC</th></tr>");
	for (var i in finalJSONArray) 
	{
		 template.print("<tr><td>" + finalJSONArray[i].URL + "</td><td>" + finalJSONArray[i].ShortDesc + "</td><td>" + finalJSONArray[i].AssignmentGroup + "</td><td>" + finalJSONArray[i].Complexity + "</td><td bgcolor=#FF0000>(finalJSONArray[i].IncGreq["true"] </td> ? <td bgcolor=yellow>(finalJSONArray[i].IncGreq["false"] </td> + "</td></tr>");
	}
	template.print("</table></p><br/>");
})(current, template, email, email_action, event);

 

1 ACCEPTED SOLUTION

Hi Hemanth,

Can you store the output of the ternary operator in variable and print that and then use that to compare

var jsonValue = finalJSONArray[i].IncGreq.toString();

var finalValue = (jsonValue == 'TRUE') ? "<td bgcolor=#FF0000>" : "<td bgcolor=yellow>";

gs.info('Ternary Operator outcome is: ' + finalValue);


 template.print("<tr><td>" + finalJSONArray[i].URL + "</td><td>" + finalJSONArray[i].ShortDesc + "</td><td>" + finalJSONArray[i].AssignmentGroup + "</td><td>" + finalJSONArray[i].Complexity + "</td>" + finalValue + jsonValue + "</td></tr>");

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

13 REPLIES 13

Hi Hemanth,

you can try using ternary operator; example below for TRUE/FALSE; you can enhance it for others

 template.print("<tr><td>" + finalJSONArray[i].URL + "</td><td>" + finalJSONArray[i].ShortDesc + "</td><td>" + finalJSONArray[i].AssignmentGroup + "</td><td>" + finalJSONArray[i].Complexity + "</td>" + (finalJSONArray[i].IncGreq.toString() == 'TRUE') ? "<td bgcolor=#FF0000>" : "<td bgcolor=yellow>" + finalJSONArray[i].IncGreq + "</td></tr>");

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur,

Thank-you for the reply. The above script is not working. I see the below output.

find_real_file.png

I even tried using the below script but it's not working.

(finalJSONArray[i].IncGreq["true"]) ? "<td bgcolor=#FF0000>" : "<td bgcolor=yellow>" + finalJSONArray[i].IncGreq + "</td>

 

Hi Hemanth,

the ternary operator should work

you should use this to get the value; the way you have added is incorrect

this is incorrect: finalJSONArray[i].IncGreq["true"] and below is the appropriate method

finalJSONArray[i].IncGreq.toString() == 'TRUE'

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur,

Please find the below snapshot of the output. The ternary operator doesn't seems to be working.

find_real_file.png

Script:

find_real_file.png

Hi Hemanth,

Can you store the output of the ternary operator in variable and print that and then use that to compare

var jsonValue = finalJSONArray[i].IncGreq.toString();

var finalValue = (jsonValue == 'TRUE') ? "<td bgcolor=#FF0000>" : "<td bgcolor=yellow>";

gs.info('Ternary Operator outcome is: ' + finalValue);


 template.print("<tr><td>" + finalJSONArray[i].URL + "</td><td>" + finalJSONArray[i].ShortDesc + "</td><td>" + finalJSONArray[i].AssignmentGroup + "</td><td>" + finalJSONArray[i].Complexity + "</td>" + finalValue + jsonValue + "</td></tr>");

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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