- 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 02:24 PM
Hi @Rohansalwi ,
I tried your script in my pdi with a catalog form. It is working as expected. Try debugging your script with alerts within 'if' condition to check if your client script is running or not. Also verify the backend names of field and choice values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-21-2025 05:27 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.
- 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