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.

Colour a field if it has any value

Eduard B1
Tera Contributor

I'm trying to colour a field when it has any value in it. I've tried using Styles, UI Policy, Client Script but I can't get it to work. Here's what I tried in client scripts:

 

function onChange() {
var fieldName = 'caller_id'; // Replace 'caller_id' with the name of your field

var fieldValue = g_form.getValue(fieldName);

// Check the field value and apply CSS styling
if (fieldValue) {
// Apply CSS styling to change the background color when the field is not empty
g_form.addDecoration(fieldName, 'bgcolor', '#00FF00'); // Change the color to light green or any other color you prefer
} else {
// Remove any existing decoration when the field value is empty
g_form.removeDecoration(fieldName, 'bgcolor');
}
}

 

Any ideas how I can make this work?

7 REPLIES 7

I tried working for me.

dgarad_0-1715231940797.png

 

dgarad_1-1715231965371.png

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad

SK Chand Basha
Tera Sage
Tera Sage

Hi @Eduard B1 

Refer this video it might be helpful

 

https://youtu.be/96kRZRsvDPQ?si=nl0SBNkmYB_MaJER

 

Mark it Helpful and Accept Solution!! If this helps you to understand.

GuruP
Tera Contributor

Hi  Eduard B1,

You can achieve this by using client script,

I color a field when priority changes.

 

var priorit = g_form.getControl('priority');
 if (newValue == '1' ){
    priorit.style.color = 'red';
 } else {
    priorit.style.color = '';
 }
 
If my answer finds you well, and related to the question asked. Please mark it as helpful.