The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Change background color of CHG assigned to

shane_davis
Tera Expert

Hi again,

My script to change the color of the Change Request's assigned to based on the assigned to's score is always coloring the assigned to field the LightGreen color even when the assigned to's score is > 4.   Do you have any ideas what I'm doing wrong?   I really need this to run onLoad for the Change Request, but my coding didn't work for that either.   Thank you so much for your continued help.   I'm going to go through w3schools.com ASAP!


Client Script

Type: onChange

Table:   Change Request

Field name:   Assigned to

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

if(isLoading){

  var callerField = $('sys_display.change_request.assigned_to');

  if (!callerField)

    return;

 

  if (!newValue) {

    callerField.setStyle({color: ""});

    return;

  }

  g_form.getReference('assigned_to', assignedtoCallback);

}

function assignedtoCallback(assignee) {

  var callerField = $('sys_display.change_request.assigned_to');

  if (!callerField)

    return;

 

  //check for Assigned to CHG failure score.

  if (assignee.u_failed_change_score <= '4') {

    callerField.setStyle({background: "LightGreen"});

  } else if (assignee.u_failed_change_score >= '5' && assignee.u_failed_change_score <= '15') {

    callerField.setStyle({background: "LightBlue"});

  } else if (assignee.u_failed_change_score >= '16' && assignee.u_failed_change_score <= '29') {

    callerField.setStyle({background: "orange"});

  } else if (assignee.u_failed_change_score >= '30') {

    callerField.setStyle({background: "red"});

  }

}

}

5 REPLIES 5

kopp820
Mega Guru

Have you looked into field styles at all? That's the route I would go. The only difference is the color will change after a save, not on demand.



http://wiki.servicenow.com/index.php?title=Defining_Field_Styles


I tried Field Styles first but I didn't know how to make the Change Request assigned to change background color due to the Failed Change Score being on the User table and the Assigned to being on the Change request form.



In addition, I need the color to be there on form load rather than after a save or any other change.


You might want to try getControl for the assigned to field.


http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#getControl



var callerField = g_form.getControl('sys_display.change_request.assigned_to');


I'm just guessing here. Client scripts get complex when dot walking, you may end up needing to do an ajax call.


http://wiki.servicenow.com/index.php?title=GlideAjax


Ignore what I said about dot walking, I didn't realize this was on the change table. I kept thinking it was on the change task table. Still try get control.