- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2024 05:32 PM - edited 03-20-2024 06:19 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 07:55 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 01:10 PM
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=
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 01:28 PM
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 03:14 PM
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... .
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 06:17 PM