How to get output into list in action in flow designer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2022 11:17 AM
(function execute(inputs, outputs) {
var completed;
var user_list=[];
var vaccinated=[];
var not_vaccinated=[];
var team = inputs.team_members;
while(team.next())
{
user_list.push(team.sys_id.toString());
}
if (user_list == '')
{
user_list.push(team.sys_id.toString());
}
for(var i=0; i < user_list.length; i++){
var user=new GlideRecord('sys_user');
user.addQuery('sys_id',user_list[i]);
user.query()
if(user.next()){
var gr_vacc_state=new GlideRecord('vaccine_status');
gr_vacc_state.addEncodedQuery('u_employee_name='+user.sys_id+'^vaccination_status=vaccinated');
gr_vacc_state.query();
if(gr_vacc_state.next()){
if(completed==null)
{
completed=user.sys_id;
}
else
{
completed=completed+','+user.sys_id;
}
vaccinated.push(user.sys_id);
}
else
{
not_vaccinated.push(user.sys_id);
}
}
}
outputs.vaccinated=completed;
outputs.not_vaccinated=not_vaccinated;
var gr=new GlideRecord('sc_req_item');
gr.addQuery('number',inputs.ritm);
gr.query();
if(gr.next())
{
gr.variables.team_members=completed;
gr.update();
}
})(inputs, outputs);
Hi,
I have a action written in flow designer,
But in here, output is always null, can someone support by identifying what's wrong in the code.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-21-2022 04:32 AM
There are a view things I notice in your script. Let me first answer your direct question on why outputs it empty:
In the arrays and string you build within the for-loop you are strong **references** to the sys_id of a gliderecord. At the end of the loop you would have **only** the very last sys_id but n-times. You will need to use toString() on the sys_ids to convert them to a string which is a copy of data retained in your string/array.
Also, the GlideRecord object will be discarded at the end of your code making all references to the sys_ids of it null.
Ok, as we have this out of the way a few more things to consider:
a) Why to you need the GlideRecord to sys_user? You are not doing anything with the retrieved data. The sys_id is already know so you can directly query the vaccine_status table.
b) Why do you make individual queries per user? One big query with a condition like
sys_id IN (sys_id1, sys_id2, sys_id3, ...)
would do the trick and reduce the database load significantly.
c) Why using a script in the first place?
Flow Designer has powerful query steps in flows/subflows and actions. No need to script any of it.
d) Updating the RITM Record at the end
What about if the RITM record doesn't exist? You do check for that but only after all the other expensive actions like loops. Do this check in the beginning and exit if the record does not exists. Again, here you can also use powerfull actions / steps in Flow Designer and need no script at all.