Color coding a change form field(cmdb_ci) with respect to the criticality of the cmdb record.

Frankline J
Tera Expert

Hi all,

I have a requirement to change the color of the cmdb_ci field in the change form to red when the criticality of the ci is business critical and amber when it is mission critical, basically we need to query the ci record to get the criticality, i have tried the following script but it is not working, please assist.

client script:

 

 

function onLoad() {
   var ciSysId = g_form.getValue('cmdb_ci');
   if (ciSysId) {
        var ga = new GlideAjax('getChangeDetails');
        ga.addParam('sysparm_name', 'getCICriticality');
        ga.addParam('sysparm_ci', ciSysId);
        ga.getXML(function(response) {
            var answer = response.responseXML.documentElement.getAttribute('answer');
            var clr = $('sys_display.' + g_form.getControl('cmdb_ci').id);
            if (answer) {
                // Set background color based on criticality value
                if (answer === 'Business Critical') {
                    clr.style.backgroundColor = 'red';
                } else if (answer === 'Mission Critical') {
                    clr.style.backgroundColor = 'amber';
                } else {
                    // Reset background color if it's not business or mission critical
                    clr.style.backgroundColor = '';
                }
            }
		}
   }
        });
   
}

 

   Script include :

 

 getCICriticality: function(ciSysId) {
    var crit = '';

    if (ciSysId) {
        var cmdbci = new GlideRecord('cmdb_ci');
        cmdbci.addQuery('sys_id', ciSysId);
        cmdbci.query();

        if (cmdbci.next()) {
            crit = cmdbci.getValue('u_component_criticality');
        }
    }

    return crit;
},

 

 

Thank you.

1 ACCEPTED SOLUTION

Frankline J
Tera Expert

I have updated the client script as below and it is working as expected.

 

var clr = g_form.getControl('sys_display.change_request.cmdb_ci'); 
            if (answer) {
                // Set background color based on criticality value
                if (answer == 'Business critical') {
					
                    clr.style.background = '#FF0000';
					//alert("Setting background to red");
                } else if (answer == 'Mission Critical') {

                    clr.style.background = '#FFBF00'; // amber code
					//alert("Setting background to amber");

                } else {
                    // Reset background color if it's not business or mission critical
                    clr.style.background = '';
                }

 

Thanks.

 

View solution in original post

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@Frankline J Update your script include as follows and check if the fix works.

 

getCICriticality: function() {
    var crit = '';
    var ciSysId = this.getParameter("sysparm_ci");
    if (ciSysId) {
        var cmdbci = new GlideRecord('cmdb_ci');
        cmdbci.addQuery('sys_id', ciSysId);
        cmdbci.query();

        if (cmdbci.next()) {
            crit = cmdbci.getValue('u_component_criticality');
        }
    }

    return crit;
},

Make sure that your script include is client callable.

 

Hope this helps.

Hi Sandeep, Thanks for you response, I have updated the script include but still the background is not getting changed.

@Frankline J Did you use the alert 

var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer);

To check if you are receiving the correct response from the server. Also, on your client script, make sure the Isolate Script checkbox is unchecked.

Utpal Dutta
Tera Guru

Hi there,

Did you try it by using field styles? If not please go to System UI > Field Styles. Select you table and field name and in value you need to either evaluate your script include or you can directly compare any field value by (current.field_name == '').

Once above is done you need to select the color of the field in Style field eg: (background-color: #D3D3D3;).

Save the record and check. I'm sure this will work for you.

 

If my solution works please mark my answer Correct.

 

Thanks,

Utpal