How to change a field style color

Honor_ Lagaza
Tera Contributor

Hello everyone, 

 

I'm working on incident table, and I want to change the service field font when the priority changes. Meaning, if the priority is critical, I want the service field font turn in red. 

 

But my code is not working : 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
  var priority = g_form.getValue('priority');

  if (priority == '1') { 
    g_form.setFieldStyles('business_service', 'color', 'red');
  
  } 
}
 
Can anyone help me please ? 
 
Thanks. 
3 REPLIES 3

Rob Sestito
Mega Sage

Hello @Honor_ Lagaza -

I am not sure you can do this using a client script, as you should see the message within a new client script form, and your script would be effected:

RobSestito_0-1725977532275.png

 

Are you able to offer the use case as to why you are looking to change the field font color to red?

 

If you are looking to make the field mandatory, you could do so easily using a UI Policy. So that when Priority changes to Critical (1), Service field becomes mandatory with a red asterisk next to it.

 

Or if you are looking to call out the field even more, you could display a message box above/below it to call out it using a client script.

 

Not Critical:

RobSestito_1-1725977846796.png

 

When changed to critical:

RobSestito_2-1725977884810.png

 

onChange Client Script:

RobSestito_3-1725978279661.png

Script (Example):

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var priority = g_form.getValue('priority');
    if (priority == '1') {
        g_form.showFieldMsg('business_service', 'Priority is Critical!', 'error');
    } else {
        g_form.hideFieldMsg('business_service', 'error');
    }
}

 

I am interested in understanding your Use Case to make the font red from the Service field.

 

If it is truly to just call it out more/make the person working on the Incident pay attention to the field. I would make it Mandatory using a UI Policy based on the Priority Field.

 

Or you could use the showfieldmsg option within a Client Script (onChange) based on the Priority Field.

 

Or even combine both. UI Policy to make the field mandatory AND display a message informing the person working the ticket.

 

I hope this helps you out -

Cheers,

-Rob

Wessel van Enk
Tera Guru
Tera Guru

Did you try to set this up in the sys_ui_style table?
See this page for more information: Define field styles (servicenow.com)

Harish Bainsla
Tera Sage
Tera Sage

Hi @Honor_ Lagaza  check below code and screenshots i done on my pdi its working

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var priority = g_form.getValue('priority');
    var labelcolor = g_form.getLabel('business_service');
    if (priority == '1') {
        labelcolor.style.color = 'red';
    } else {
        labelcolor.style.color = '';
    }

}
HarishBainsla_0-1726048547500.png

 

if my answer help please mark helpful and accept solution