background color of field not working in client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2022 10:19 PM
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";
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2024 08:48 AM - edited 08-15-2024 08:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2022 10:41 PM
Hi
Alongwith the suggestion given by
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-20-2022 10:57 PM
Hi
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
Then add the required configuration referring example shown below:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2023 10:43 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2023 11:04 PM
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