How do I change the field background colour?

ChuanYanF
Tera Guru

Dear experts,

 

I would like to know how do I configure the background label colour for a field to be like this?

ChuanYanF_0-1755487568915.png

Currently my script is like this but it does not show the color like the background I wanted above. How do i change my script so that my field whole background becomes red or green instead of gray now.

ChuanYanF_1-1755488033033.png

 

2 ACCEPTED SOLUTIONS

@ChuanYanF 

 

I do not think issue is with style or client script. Check your business rule where you set field value of 'u_final_scoring' or create a manual record and set the value of the field to 1 or 2 and test it.

 

Below is for reference where I simulated your use-case in my PDI,

Bhuvan_0-1755660392281.pngBhuvan_1-1755660406682.png

Bhuvan_4-1755660707083.png

Bhuvan_2-1755660423341.pngBhuvan_3-1755660433376.png

I hope you appreciate the efforts to mimic this scenario in my PDI and provide you with this  information by marking the posts helpful and accept the solution.

 

Thanks,

Bhuvan

View solution in original post

@ChuanYanF 

 

Glad to know it is working now. I can see there is a known issue in applying field styles for read-only fields and problem record is PRB663205. Not sure if enhancement request for this feature is made available,

 

https://www.servicenow.com/community/servicenow-ai-platform-forum/feature-request-apply-styles-to-re...

 

I have seem some community solutions but not sure if it would work and would recommend to uncheck 'Read Only' from dictionary and if you want to enforce it, do so from UI Policy.

 

See below for sample UI policy, change filter conditions to suit your requirement

Bhuvan_0-1755679096101.png

'u_final_scoring' field is read-only and styles would work with this option,

Bhuvan_1-1755679175837.png

I hope you appreciate the efforts in providing this solution and would mark the post helpful and accept the solution.

 

Thanks,

Bhuvan

View solution in original post

24 REPLIES 24

@ChuanYanF 

 

Set the style definition, go to table -> field -> configure Styles and create entries as below

Bhuvan_3-1755500034857.pngBhuvan_4-1755500055323.png

Bhuvan_5-1755500068457.png

javascript: current.u_final_scoring == "Yes"
background-color: tomato;

javascript: current.u_final_scoring == "No"
background-color: green;

 

Create Client Script as below,

Bhuvan_6-1755500103536.png

function onLoad() {
//Type appropriate comment here, and begin script below
var finalScore = g_form.getControl('u_final_scoring');
if (finalScore.value == "Yes")
{
finalScore.style.backgroundColor="tomato";
}
if (finalScore.value == "No")
{
finalScore.style.backgroundColor="green";
}
}

 

Sample output,

Bhuvan_7-1755500178461.png
Bhuvan_8-1755500197551.png

 

If this helped to answer your query, please mark my posts helpful & accept the solution to close the thread.

 

Thanks,

Bhuvan

Hi Bhuvan, I still cannot be able to generate what u did, I copied pasted each steps but it still remain gray rather than a coloured field. Isit because I am using business rule to populate my answer on final_scoring?

Using a ClientScript shouldt be necessary.
You can script conditions in the value field of your style:

Screenshot 2025-08-18 091626.png

In your case you should check the string or define it as choice field and check the value.
Friendly reminder: It is considered best practice in most cases to define values of choice fields as integer

Please mark answers (not only mine) as helpful if they were
and "accepted solutions"This motivates others to take part, post solutions and find answers. Thanks! - Mat

It still only will show at the list level, but not the form level. How should I approach this issue?

ChuanYanF_0-1755504435237.png

 

@ChuanYanF 

 

Please share screenshots of what you configured in styles and client script.

 

Thanks,

Bhuvan