Dynamically Change Field Color Based on u_sla

nabil
Tera Contributor

Hello everyone,

I'm working on a configuration in ServiceNow to dynamically change the color of a field based on the percentage value (u_sla) in the incident table. My goal is to make it so that:

  • When u_sla is between 0% and 50%, the field is green.
  • When u_sla is between 51% and 99%, the field is orange.
  • When u_sla is 100% and above, the field is red.
    nabil_0-1731608894725.png

     


    Additional Details:

    • The u_sla field is of type string and is currently read-only.
    • The u_sla field retrieves its information from the task_sla table.

      What I’ve Tried So Far

      1. Field Styles and Script Include:

        • I created a Script Include called ColorCheck to return the appropriate color based on the u_sla value.

        • In each Field Style, I called the function ColorCheck().getColor() to check the value and apply the corresponding style.

        • Here’s the script I used in my Script Include:

          var ColorCheck = Class.create();
          ColorCheck.prototype = {
          initialize: function() {},

          getColor: function(value) {
          var slaValue = parseFloat(value);
          if (isNaN(slaValue)) return '';

          if (slaValue >= 0 && slaValue <= 50) {
          return 'green';
          } else if (slaValue > 50 && slaValue < 100) {
          return 'orange';
          } else if (slaValue >= 100) {
          return 'red';
          }
          return '';
          },

          type: 'ColorCheck'
          };

          For each Field Style (green, orange, red), I used a condition in the Value field as follows:

          • Green (0% to 50%):

            javascript: new ColorCheck().getColor(current.u_sla) == 'green';
          • Orange (51% to 99%):

            javascript: new ColorCheck().getColor(current.u_sla) == 'orange';
          • Red (100% and above):

             
            javascript: new ColorCheck().getColor(current.u_sla) == 'red';

            Despite this configuration, the u_sla field does not change color
            Could anyone help me identify what might be missing or configured differently to achieve the dynamic color change?
            Would there be a better approach to meet this requirement?

            Thanks in advance for any help or suggestions!




1 ACCEPTED SOLUTION

JenniferRah
Mega Sage

My SLA field was a string, because I was trying to make it just like you said in your original post. 

 

If the colors are appearing in the list view, that means the script must be returning the correct value, so it should be working in the form view. The field does have to be read only for it to show, but you said yours is read only, correct? If you try this in your PDI, does it work? If so, there might be some competing code in your instance. Maybe another field style or business rule?

View solution in original post

10 REPLIES 10

JenniferRah
Mega Sage

What are you using for the Style field in your field style record? I have some with conditions that are using something like this and they work: 

background-color: tomato

 

nabil
Tera Contributor

Thank you for your response, I don't think that's the source of the problem.I also tried with Tomato.

Can you include a screen shot of your Field Style?

nabil
Tera Contributor

yes

nabil_0-1731693618473.png