- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2022 07:48 AM
Hi,
I have an issue, in below action script in flow designer. I have used this action in flow.
This is written to check whether team members are vaccinated and mandatory course completed. If so, they are considered as eligible users, if not they are non eligible users.
Below code works if there's only one user/ if I add multiple users (non eligible users + eligible users) this is not working and giving below error.
(function execute(inputs, outputs) {
var pandemic_completed=[];
var fully_vaccinated=[];
pandemic_completed=inputs.pandemic_completed_users.split(',');
fully_vaccinated =inputs.fully_vaccinated_users.split(',');
var eligible_users=[];
var isFound=null;
for(var i=0;i<pandemic_completed.length;i++)
{
isFound=fully_vaccinated.indexOf(pandemic_completed[i]);
if(isFound!=-1)// if element is found
{
gs.info('this element : '+pandemic_completed[i] +' exists');
if(eligible_users==null)
{
//eligible_users=pandemic_completed[i];
eligible_users.push(pandemic_completed[i]);
}
else
{
eligible_users.push(pandemic_completed[i]);
}
}
else{
eligible_users=null;
}
}
//}
gs.info('Eligible users '+eligible_users);
var gr=new GlideRecord('sc_req_item');
gr.addQuery('number',inputs.ritm);
gr.query();
if(gr.next())
{
if(eligible_users != null)
{
if(eligible_users.length == 1)
{
gr.variables.team_members=eligible_users.join(',');
}
else
{
gr.variables.team_members=eligible_users.join(',');
}
}
else
{
gr.variables.team_members=null;
}
gr.update();
}
})(inputs, outputs);
Please give some guidance to resolve this issue. Thanks in advance.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2022 08:23 AM
Hi,
We'd love to get a bit more information from you such as any log statements you've tried, where you think it's working and where you think it starts to break down (this to help you learn).
However, from my quick assessment, you have this (which is more of a clean-up item):
if(eligible_users==null)
{
//eligible_users=pandemic_completed[i];
eligible_users.push(pandemic_completed[i]);
}
else
{
eligible_users.push(pandemic_completed[i]);
}
So that's the same thing after you commented out that one line, so consider cleaning this up and not using the else.
Additionally, you have this:
if(isFound!=-1)// if element is found
{
//do 'x'
}
else{
eligible_users=null;
}
So breaking this part down, if you find the same entry (I'm assuming user) from both arrays: fully_vaccinated and pandemic_completed, then you do 'x', which in 'x'...you're adding the user to the eligible_users array, HOWEVER...if they're not found in both the fully_vaccinated and pandemic_completed arrays, then you're dumping/clearing the eligible_users array....
So...that's not good. You should just not add them to the eligible_users array and move on, there's no need to dump the entire eligible_users array. So you can change this code section to just be:
if (isFound!=-1) {
gs.info('this element : '+pandemic_completed[i] +' exists');
eligible_users.push(pandemic_completed[i]);
}
gs.info('Eligible users '+eligible_users);
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
‎03-08-2022 08:23 AM
Hi,
We'd love to get a bit more information from you such as any log statements you've tried, where you think it's working and where you think it starts to break down (this to help you learn).
However, from my quick assessment, you have this (which is more of a clean-up item):
if(eligible_users==null)
{
//eligible_users=pandemic_completed[i];
eligible_users.push(pandemic_completed[i]);
}
else
{
eligible_users.push(pandemic_completed[i]);
}
So that's the same thing after you commented out that one line, so consider cleaning this up and not using the else.
Additionally, you have this:
if(isFound!=-1)// if element is found
{
//do 'x'
}
else{
eligible_users=null;
}
So breaking this part down, if you find the same entry (I'm assuming user) from both arrays: fully_vaccinated and pandemic_completed, then you do 'x', which in 'x'...you're adding the user to the eligible_users array, HOWEVER...if they're not found in both the fully_vaccinated and pandemic_completed arrays, then you're dumping/clearing the eligible_users array....
So...that's not good. You should just not add them to the eligible_users array and move on, there's no need to dump the entire eligible_users array. So you can change this code section to just be:
if (isFound!=-1) {
gs.info('this element : '+pandemic_completed[i] +' exists');
eligible_users.push(pandemic_completed[i]);
}
gs.info('Eligible users '+eligible_users);
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!