Setting a field style via client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2022 09:35 AM
Hello,
I am trying to do something I feel should be very simple but I have been unable to get this to work.
I need to change the font color of the text in a reference field within the form (not on the list view) based on a condition. Field Styles will not work as they only apply to the list view when a condition is applied.
The script i am trying to use is based on this community post (https://www.servicenow.com/community/developer-forum/how-to-get-styles-to-work-on-a-form-field-not-j...) however it does not work.
The code:
var myField = 'u_requested_for';
var ctrl = $('sys_display.' + g_form.getControl(myField).id);
ctrl.style.fontWeight = 'bold';
ctrl.style.backgroundColor = 'lightBlue';
This does nothing when I apply it to the sc_req_item table on a reference field, any reference field. I am not even applying a condition as of yet. I have put this within an onLoad script.
My intention is to change the font color to red when the user within the opened_by or a custom reference field "u_requested_for" is a VIP. We are on San Diego if that makes any difference.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2022 09:44 AM - edited ‎11-29-2022 09:47 AM
Hi,
Make sure the field name is correct. Right click on field/column and show click field_name, copy it. Add it to scripts.
var myField = 'u_requested_for'; // add correct name here
var ctrl = $('sys_display.' + g_form.getControl(myField).id);
ctrl.style.fontWeight = 'bold';
ctrl.style.backgroundColor = 'lightBlue';
OR may be you can add Field styles as -
Table - Table name
Field name - select field name
Value - add condition if required.
Style -
background-color: lightBlue;
font-size: 24px
Thanks,
Sagar Pagar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2022 09:46 AM
the field name is correct. I have tested this on several fields and no matter what the style change is not applied to any field on this table.