- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-14-2024 10:31 AM
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.
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
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!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-18-2024 05:38 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2024 10:32 AM - edited ‎11-15-2024 10:37 AM
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?
You may need to check the ACL on your script include to make sure it's getting executed correctly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-15-2024 01:23 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-16-2024 05:37 PM
Hey ,
The requirement is to have the colors applied directly to the field in the form, using the colors specified above.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-17-2024 08:08 PM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-18-2024 05:38 AM
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?