Make field color base on the option selected

johnsony2
Tera Contributor

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.

7 REPLIES 7

Hi,

Is the form being displayed in Service portal?

.getControl() only works in U16.

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

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

Abhay Kumar1
Giga Sage
Hi John, you can follow below article to have answer of you question, https://community.servicenow.com/community?id=community_question&sys_id=abf9cf29db5cdbc01dcaf3231f961951