background color of field not working in client script

sunil7
Giga Expert

Hi everyone , I want to change the background color of field Resudual Risk(u_priority) string field with choices , Writing one onload client script but not working please see below

 

Code : onload client script 

code is going inside if and getting alert but background color is not changing

var element = g_form.getElement('u_priority');
   // alert(element);
    var accessType = g_form.getValue('u_priority');
    if (accessType == 'Low') {
   alert(accessType);
        element.style.backgroundColor = "LimeGreen";
    }
    if (accessType == 'Medium') {
  alert(accessType);
        element.style.backgroundColor = "yellow";
    }
    if (accessType == 'High') {
	alert(accessType);
        element.style.backgroundColor = "orange";
    }
    if (accessType == 'Well Controlled') {
  alert(accessType);
        element.style.backgroundColor = "green";
    }
    if (accessType == 'Critical') {
  alert(accessType);
        element.style.backgroundColor = "red";
    }

10 REPLIES 10

Hi Ankur,

 

I tried above script its working for priority field but not for Number field.. is there any reason ?

 

both number and priority are read only

Number type string and read only at dictionary level

Priority type integer and read only with UI policy

 

Thanks

Roopa

Kartik Sethi
Tera Guru
Tera Guru

Hi @sunil 

 

Alongwith the suggestion given by @Ankur Bawiskar and @Sagar Pagar you also need to me make Isolate Script false on the Client Script.

 

Also, you can follow my older reply "How to set a field's background colour as RED" and this might be helpful to solve your issue.


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

Hi @sunil 

 

Could you please specify the reason why you are opting for Client Script?

There is an OOTB configuration available in ServiceNow that you can and should use.

Go to Navigator System UI ► Field Style

find_real_file.png

Then add the required configuration referring example shown below:

find_real_file.png

This will enable color-coding on List and Form both.


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

rushabhgupta
Tera Contributor

i have tried this , but if the field is readonly it is not working and if i uncheck the readonly option then we can see the background color

Hi @rushabhgupta,

 

For read-only field, background color will not apply.

 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow