- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2019 02:20 PM
Hey peeps,
I am trying to change a catalog item variable's bg color on RITM and Task.
My business case is:
I have a catalog variable X on RITM (which is readonly) and other variable Y which is entered by user (on RITM). If value(X != Y), then Y's bg clor should be changed. (I am not doing anything on the catalog form).
I tried something like this on catalog client script (with applies on ritm and task checkbox checked), but I guess it only works on form but not on RITM.
g_form.getControl('variable_name').style.backgroundColor = 'LimeGreen';
Any thoughts?
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2019 06:48 PM
Hi,
Per this: https://hi.service-now.com/kb_view.do?sysparm_article=KB0547171
It's a known issue with using getControl for variables where it works on ordering, but doesn't after due to the conversion of how "g_form" is perceived by the system.
This code works:
var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;
var myControl = form.getControl('variable_name');
myControl.style.backgroundColor = 'LimeGreen';
And if you use those initial two lines every time, it'll go with the correct one that's needed.
Please mark reply as Helpful/Correct. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-15-2019 06:48 PM
Hi,
Per this: https://hi.service-now.com/kb_view.do?sysparm_article=KB0547171
It's a known issue with using getControl for variables where it works on ordering, but doesn't after due to the conversion of how "g_form" is perceived by the system.
This code works:
var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;
var myControl = form.getControl('variable_name');
myControl.style.backgroundColor = 'LimeGreen';
And if you use those initial two lines every time, it'll go with the correct one that's needed.
Please mark reply as Helpful/Correct. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2019 01:43 PM
Hi Allen,
Quick question, I have this code in my OnChange client script, it is working fine on change but when I save the form, the color doesn't retain.. here is my code and some screenshots of behavior. am I missing something? any thoughts?
When I change the value:
But when I save the form (sc_task)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2019 01:51 PM
That's because this is set for onChange...you'd need this script to also run for onLoad...which is what you're doing when you save and are back on the form, you're reloading it. So since you're using prev and newvalue in this...you can't really reuse or tweak this onChange script, instead...you can create an onLoad client script that will automatically load the color per the values contained.
Without knowing the full context here...when you save and reload the form...at that exact moment, there is no previous value or new value...so you'd need to write your script accordingly.
Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-22-2019 05:09 PM
Hi Alan -
Thank you for this - it solved an issue I was really struggling with. 🙂 Except...
I've got a catalog item where users are requesting changes to existing records, and the form display the 'From' and 'To' values. If they're different, the 'To' variable gets highlighted. It's working on all the variables except for one, and I can't figure out why. The one it's not working on is a reference variable, while the others are strings.
if (g_form.getValue('customer_name')==g_form.getValue('to_customer_name')){
g_form.setValue('to_customer_name',"");
} else {
highlightVariable('to_customer_name');
}
<rows for other variables on the item go here>
function highlightVariable(variable){
var form = typeof g_sc_form != "undefined" ? g_sc_form : g_form;
var myControl = form.getControl(variable);
myControl.style.backgroundColor = 'lightblue';
}
When I add alerts to the highlight function, they display, but the highlighting doesn't happen. Any idea what's going on?
Thank you for your time.