Split function not working in script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2021 07:12 AM
Hello Team,
I tried putting logs and got an error message 'Split function not working'
"org.mozilla.javascript.EcmaError: Cannot find function split in object ".
Script:
answer = [];
SwissTriggerApproval();
function SwissTriggerApproval()
{
var affCIs = new GlideRecord('task_ci');
gs.log('Hello ' +affCIs);
affCIs.addQuery('task', current.sys_id);
affCIs.query();
while(affCIs.next())
{
//answer.push(affCIs.ci_item.u_task);
var list = current.u_watch_list_group;
gs.log('Hello1 ' +list);
var array = list.split(",");
for (var i=0; i < array.length; i++)
{
answer.push(array[i]);
gs.log('Hello2 ' +array[i]);
}
}
}
Error1:
org.mozilla.javascript.EcmaError: Cannot find function split in object .
Caused by error in sys_ui_action.f07eedb3cb200200d71cb9c0c24c9c3d.script at line 14
11: //answer.push(affCIs.ci_item.u_task);
12: var list = current.u_watch_list_group;
13: gs.log('Hello1 ' +list);
==> 14: var array = list.split(",");
15: for (var i=0; i < array.length; i++)
16: {
17: answer.push(array[i]);
Error 2:
org.mozilla.javascript.EcmaError: Cannot find function split in object .
Caused by error in sys_ui_action.f07eedb3cb200200d71cb9c0c24c9c3d.script at line 17
14: var array = list.split(",");
15: for (var i=0; i < array.length; i++)
16: {
==> 17: answer.push(array[i]);
18: gs.log('Hello2 ' +array[i]);
19: }
20: }
org.mozilla.javascript.EcmaError: Cannot find function split in object .
Caused by error in sys_ui_action.f07eedb3cb200200d71cb9c0c24c9c3d.script at line 17
14: var array = list.split(",");
15: for (var i=0; i < array.length; i++)
16: {
==> 17: answer.push(array[i]);
18: gs.log('Hello2 ' +array[i]);
19: }
20: }
PLease provide some inputs so that it can be resolved.
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2021 07:17 AM
Hello,
Please paste your code in to your post using the forum feature: Insert/Edit code sample.
This makes it easier for us to read...thanks!
Please mark reply as Helpful, if applicable. 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
‎04-12-2021 07:20 AM
answer = [];
SwissTriggerApproval();
function SwissTriggerApproval()
{
var affCIs = new GlideRecord('task_ci');
gs.log('Hello ' +affCIs);
affCIs.addQuery('task', current.sys_id);
affCIs.query();
while(affCIs.next())
{
//answer.push(affCIs.ci_item.u_task);
var list = current.u_watch_list_group;
gs.log('Hello1 ' +list);
var array = list.split(",");
for (var i=0; i < array.length; i++)
{
answer.push(array[i]);
gs.log('Hello2 ' +array[i]);
}
}
}
OK

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-12-2021 08:16 AM
Hi,
Your script doesn't make a lot of sense as it is querying the task_ci table and looking for task_ci records that have the same task as the current record's sys_id. Then, you're looping through all the matched records, but then setting a JavaScript variable called: "list", to the current records u_watch_list_group field (so not the looped records u_watch_list_group field....but the current record, which you already have it).
So what was the point of the while loop then if you're not using anything from those records?
Perhaps you can explain fully what you're trying to do so we can help?
Please mark reply as Helpful/Correct, if applicable. 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
‎04-12-2021 07:23 AM
This error occurs if there are no values in the watch_list_group field for the record in while loop.
Enclose your split function and the for loop inside a condition:
var list = current.u_watch_list_group;
if(list.toString().length != 0)
{
//Put the rest of the code here
}
Long Live ServiceNow!