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

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.