Utility function for getting sys_ids, emails, and names in Flow Designer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2024 11:46 AM
I built this to help in my Workflow Studio Actions and it's really been fantastic. Feel free to share your thoughts with me on this.
Inputs:
Then created 3 Script actions:
In the Get User IDs Script:
- I had one input variable called role_name and it was populated by the Role input from above
Then this script:
(function execute(inputs, outputs) {
var role_name = inputs.role_name;
var role_query = 'name=' + role_name;
var role_gr = new GlideRecord('sys_user_role');
role_gr.addEncodedQuery(role_query);
role_gr.query();
role_gr.next();
var role_id = role_gr.sys_id;
var encodedQuery = 'role=' + role_id;
var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery(encodedQuery);
gr.query();
var users = [];
while (gr.next()) {
users.push(gr.user.toString());
}
if (users.length == 0) {
gs.log('No users in this group', "error") ;
}
outputs.users_string = users.join(',');
outputs.users_array = users;
})(inputs, outputs);
And then obviously two outputs:
- users_string
- users_array
The Get Emails step takes two inputs:
- get_emails (the boolean from the input variables)
- ids (an array of sys_ids from Get User IDs)
Then this script:
(function execute(inputs, outputs) {
if (!inputs.get_emails) {
return;
} else {
var user_ids = inputs.ids;
var email_list = [];
var missing_email_ids = [];
var missing_count = 0;
var i = 0;
while (i < user_ids.length) {
var gr = new GlideRecord('sys_user');
gr.get(user_ids[i]);
if (gr.email) {
email_list.push(gr.email.toString());
}
else if (!gr.email) {
missing_email_ids.push(user_ids[i])
missing_count += 1;
}
i++;
}
outputs.email_array = email_list;
outputs.email_string = email_list.join(',');
outputs.missing_email_ids = missing_email_ids;
outputs.missing_emails = missing_count;
}
})(inputs, outputs);
Then the outputs:
- email_array
- email_string
- missing_email_ids (a list of sys_ids where no email could be retrieved)
- missing_emails (a count of missing email addresses)
I repeated the Get Emails step for Get names, only replacing email with name throughout.
This utility action allows for you to get all email addresses and names for a role and use them as data in your flows
I hope you guys liked it.