Help with Field Styles please!

JoeS43464908901
Tera Contributor

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 

1 ACCEPTED SOLUTION

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

View solution in original post

9 REPLIES 9

Hey @JoeS43464908901,

 

I am assuming you have the 'isLoading' in the first if statement.

Delete that and it should work onLoad as well, if it still doesn't work, share the script, please.

 

Cheers

Did not work. Here's what I'm working with (have this on the Incident table too, see image). 


function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    var ciValue = g_form.getValue('u_configuration_item');

    // Get the install status of the selected CI
    var installStatus = '';
    if (ciValue) {
        var ciGR = new GlideRecord('u_configuration_item');
        if (ciGR.get(ciValue)) {
            installStatus = ciGR.install_status.getDisplayValue();
        }
    }

    // If the install status is 'Retired', set the text color of the CI field to red
    if (installStatus === 'Retired') {
        // Set the text color of the CI field to red
        g_form.getControl('u_configuration_item').style.color = 'red';
    } else {
        // Reset the text color to default if it's not retired
        g_form.getControl('u_configuration_item').style.color = '';
    }
}



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

Thank you, it worked! Though like you said, not in Workspace view.  Is there a way I could apply this to all views? 

Hey @JoeS43464908901, no worries.

I could be wrong but I believe DOM manipulation doesn't work on Workspace.

 

But you can still use something like

g_form.showFieldMsg('u_configuration_item', 'This CI has been retired', 'info', false);

This works on both UIs, thanks