How to get a specific list of groups a user belongs to and create catalog task tickets for each group?

Dazler
Mega Sage

Hi,

I hope someone is able to assist me.  I am very new to ServiceNow and I have been tasked with the orchestration part because of my skills with powershell.

I am task with setting up a termination process.  I have been able to use many of the activities in my workflow that allow updates to Active Directory, my final task to this process is to create catalog task tickets for each group that a user belongs to.  The CATCH is that we only need certain groups to have catalog task tickets created.  

Allow me to explain:

We have many groups that are assigned to each user, the naming convention for the ones that I need to create catalog task tickets for all start with _ACCESS (see example below).

_ACCESS DocMagic

_ACCESS New Hope

These are just an example of a couple of groups.

How can I get just those groups (that start with _ACCESS) and create a catalog task ticket for each one?

I searched around and can't seem to find anything close to what I need.  Any help would be appreciated.

1 ACCEPTED SOLUTION

Okay, this should work in a workflow 'Run Script' activity.

var userID = current.variable_pool.employee_name;
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('user', userID);
grp.addQuery('group.name', 'STARTSWITH', '_ACCESS');
grp.query();
while (grp.next()) {
    // Create 'sc_task' for each group found
    var tsk = new GlideRecord('sc_task');
    tsk.initialize();
    tsk.request_item = current.sys_id;
    tsk.short_description = 'Offboard ' + grp.user.getDisplayValue() + ' from ' + grp.group.getDisplayValue() + '.';
    tsk.assignment_group = grp.getValue(group);
    tsk.insert();
}

View solution in original post

7 REPLIES 7

Mark Stanger
Giga Sage

You can use a workflow 'Run Script' activity to do this.  If you can let me know how I identify which groups I need (are we just querying from the 'sys_user_group' table or are they coming from a variable or something else) and what's the name of the reference variable that stores the name of the user we're grabbing groups for, I can work up a script for you to try.

Hi, thank you for responding back.

The way our process works is our HR team fills out a service catalog request form and selects the employee name from a reference field "employee_name"

We can query from the sys_user_group table.  I could also pull it using a powershell activity but I want to pull it from the sys_user_table.

Based on the user that HR select, we want to pull all the groups that start with _ACCESS and then create a catalog task ticket for each group.

Any help you can give would be truly appreciated.

Okay, this should work in a workflow 'Run Script' activity.

var userID = current.variable_pool.employee_name;
var grp = new GlideRecord('sys_user_grmember');
grp.addQuery('user', userID);
grp.addQuery('group.name', 'STARTSWITH', '_ACCESS');
grp.query();
while (grp.next()) {
    // Create 'sc_task' for each group found
    var tsk = new GlideRecord('sc_task');
    tsk.initialize();
    tsk.request_item = current.sys_id;
    tsk.short_description = 'Offboard ' + grp.user.getDisplayValue() + ' from ' + grp.group.getDisplayValue() + '.';
    tsk.assignment_group = grp.getValue(group);
    tsk.insert();
}

Awesome, thank you!  I will give it a try and let you know how it turns out.