Help with Field Styles please!

JoeS43464908901
Tera Contributor

Hi everyone, 

I'm trying to figure out why my field style script is not turning whatever 'retired' CI that is in the CI field on an interaction red. The fields below are the correct dot walk values. Not sure what I'm missing. Anything I can try? 

Value: current.u_configuration_item.install_status.getDisplayValue() == 'Retired';

Style:   color:red 

1 ACCEPTED SOLUTION

Try this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    var ctrl = $('sys_display.' + g_form.getControl('u_configuration_item').id);
    if (newValue === '') {
		ctrl.style.backgroundColor = '';
        return;
    }

    // Get the install status of the selected CI
    var installStatus = '';
    if (newValue) {
        var ciGR = new GlideRecord('cmdb_ci');
        if (ciGR.get(newValue)) {
            installStatus = ciGR.install_status.toString();
        }
    }
    // If the install status is 'Retired', set the text color of the CI field to red
    if (installStatus == '7') {
        // Set the text color of the CI field to red
        ctrl.style.backgroundColor = 'red';
    } else {
        // Reset the text color to default if it's not retired
        ctrl.style.backgroundColor = '';
    }
}

 

A few things to note:

  • You must unselect 'Isolate script' in the Client Script
  • Using GlideRecord API on client script is not recommended. Consider using AJAX or g_scratchpad variable
  • Since this is manipulating DOM, I wouldn't recommend it. It's likely that this won't work on other UIs such as Workspace.
  • Consider using g_form.addErrorMessage or g_form.showFieldMsg

 

Cheers

View solution in original post

9 REPLIES 9

Tony Chatfield1
Kilo Patron

Hi, I think your value test needs to be prefixed javascript: eg

javascript:current.u_configuration_item.install_status.getDisplayValue() == 'Retired';

I would personally not use getDisplayValue() and would match to the actual field value,
but otherwise I see no reason why it would not work as you have it currently configured.
/sys_ui_style_list.do?sysparm_query=&sysparm_view=

Thanks for the reply, Tony. I added javascript&colon, just didn't include it in this post since it wouldn't post correctly on this forum.  I tried matching to the choice value of the field, and still, nothing. Also, I think I saw on the forum in a previous post that if choice value is used, the CSS changes would only be shown in list view and not on a form. 


 javascript: current.u_configuration_item.install_status == 3;

Apparently, it can only apply to list view and not the form itself per https://www.servicenow.com/community/developer-forum/how-to-get-styles-to-work-on-a-form-field-not-j... . 

JoeS43464908901
Tera Contributor

I created the Field Style for it to occur in list view. But could I get assistance creating the onChange client script for this? Can't get mine to stay red after the record is saved.