- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 10:07 AM
I am writing a server script similar to this instead i have a List field that holds multiple items and I want to count how many items are in that list when the list is populated using
problem_data.list_field = gr.getValue('list_field').split(',').length; works FINE , BUT when it is NULL i get an error
Please help not sure how to get around an empty value
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 10:31 AM
Hi Thomas,
I propose the following:
if ( !(gr.list_field.nil() ) {
problem_data.list_field = gr.getValue('list_field').split(',').length;
} else {
problem_data.list_field = 0;
}
Best Regards,
Marcin
If my answer helped you in any way, please mark this answer as helpful and correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 10:31 AM
Hi Thomas,
I propose the following:
if ( !(gr.list_field.nil() ) {
problem_data.list_field = gr.getValue('list_field').split(',').length;
} else {
problem_data.list_field = 0;
}
Best Regards,
Marcin
If my answer helped you in any way, please mark this answer as helpful and correct.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 01:09 PM
Thank you so much this worked!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 11:53 AM
Hi Thomas,
Try this > gr.getValue('list_field').split(',').length || ' ';
Mark Correct or Helpful if it helps.
Thanks,
Yousaf
***Mark Correct or Helpful if it helps.***

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2022 11:57 AM
If that doesn't work
Use this custom getValueAK function that also supports dot-walking. Include this function in the script itself, or dump this into a Script Include:
// You can put this in a Script Include
// Features:
// - always returns a string
// - can dot-walk
// - returns empty string instead of null (for both empty fields and non-existent fields)
// - returns string 'true' or string 'false' for boolean fields
function getValueAK(field, ge) {
// Local vars
var fields;
// If you don't pass in anything, you get an empty string
if (!field) {
return '';
}
// If no second parameter, assume this
if (!ge) {
ge = this;
}
// Split fields on dot
fields = field.split('.');
// If there is only one field
if (fields.length === 1) {
// If the field exists, return a string, else return empty string
return ge[field] !== undefined ? String(ge[field]) : '';
} else {
// If there is more than one field, recurse
return this.getValueAK(fields.slice(1).join("."), ge[fields.slice(0, 1)]);
}
}
Reference : https://community.servicenow.com/community?id=community_question&sys_id=d71d8f69db9cdbc01dcaf3231f96...
***Mark Correct or Helpful if it helps.***