Make field color base on the option selected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2022 03:27 PM
Please I have a requirement to create a field on Requested item table thus;
"Project health" with choice with colors
i. On Tract ----- green
ii. At Risk -------yellow
iii. Off track-----red.
Please I need client script code for this.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2022 03:12 AM
Hi,
Is the form being displayed in Service portal?
.getControl() only works in U16.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2022 05:28 PM
Hi,
If displaying in Service Portal, use the following script. The name of the field I've used is 'project_health'. If the name is different, would need to change it in the script.
I've tested on San Diego. It may be necessary to change the "baseId" on Rome. From the web browser, right click on the field and do an inspect to find the id of the choice field.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
var color = 'transparent ';
if (newValue == 'on track') {
color = "green";
} else if (newValue == 'at risk') {
color = 'yellow';
} else if (newValue == 'off track') {
color = 'red';
}
if (window == null && this != null) {
var baseId = 's2id_sp_formfield_';
var el = this.document.getElementById(baseId + 'project_health'); // replace with name of choice field
el.style.backgroundColor = color;
} else {
var field = g_form.getControl('project_health'); // name of choice field
field.style.backgroundColor = color;
}
}
Result
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2022 05:02 AM