- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 11:37 AM
I would like to have a field value set to Billable if another field has a value that starts with E and Non-billable if that field has a value that starts with P,I,O. I tried below onChange script but it isn't working.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2025 06:21 AM
Since this is for catalog item and the variable is of type Reference, you need to get the display value and then check the char
Something like this will work in native + portal
I created blog for this in past
Get Display value of reference variable Service Catalog
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
try {
if (window == null) {
var valuePortal = g_form.getDisplayValue('variableName');
var firstChar = valuePortal.charAt(0).toUpperCase();
if (firstChar === 'E') {
g_form.setValue('billable_status', 'Billable');
} else if (['P', 'I', 'O'].includes(firstChar)) {
g_form.setValue('billable_status', 'Non-billable');
}
}
} catch (ex) {
var valueNative = g_form.getDisplayBox('variableName').value;
var firstChar = valueNative.charAt(0).toUpperCase();
if (firstChar === 'E') {
g_form.setValue('billable_status', 'Billable');
} else if (['P', 'I', 'O'].includes(firstChar)) {
g_form.setValue('billable_status', 'Non-billable');
}
}
//Type appropriate comment here, and begin script below
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 12:14 PM
Hi @Rohansalwi
what is the field type where you will be checking the first character?
I don’t consider this as a good practice… from the design architecture it sounds very weak and i would encourage you to think of some better alternatives.
I don’t know the details but if you are fine sharing them, do so and we cam discuss it
/* If my response wasn’t a total disaster ↙️ ⭐ drop a Kudos or Accept as Solution ✅ ↘️ Cheers! */
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2025 06:11 AM
The variable from where the script is reading the project code is a reference type field. I think it needs to be converted to a string first. I tried below as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 12:14 PM
Hi @Rohansalwi ,
Try this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
alert(newValue);
var firstChar = newValue[0].toUpperCase();
if (firstChar == 'E') {
g_form.setValue('billable_status', 'Billable');
} else if (['P', 'I', 'O'].includes(firstChar)) {
g_form.setValue('billable_status', 'Non-billable');
} else {
g_form.clearValue('billable_status');
}
}
I have added an alert statement just wanted to check if the client script is even triggering
If it's coming please check if you have selected the correct variable name, catalog name, type of client script in the client script. Please share the screenshot of the client script of it's not throwing the alert
Regards
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-20-2025 12:44 PM
Also check if the billable_status variable choices are correct you should be using the value of the choice not the label
Regards
Chaitanya