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

Your code works for me. If you look at your records in a list view and show the SLA field, does it shows a dot next to it in the right color?

JenniferRah_0-1731695054728.png

You may need to check the ACL on your script include to make sure it's getting executed correctly.

nabil
Tera Contributor

Hello JenniferRAh

No, I only have the numeric value.
What is the field type for SLA? (Is it a string?) I don’t quite understand why it’s not working.
Do you have the same result on the form?

Thank you for your responses.

nabil
Tera Contributor

Hey , 

The requirement is to have the colors applied directly to the field in the form, using the colors specified above.

nabil
Tera Contributor

Hello, 

 

I can see the colors appearing on the list view now, but they still don’t appear on the field in the form.

Thank you in advance for your help!

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?