how to add user to groups via catalog workflow?

Laxmi23
Tera Contributor

I have a requirement where i need to add the user to a groups via workflow. The field in the catalog item Groups(sys_user_group) is of type (list collector).another
The field in the catalog item user(sys_user) is type reference.when i select 2 groups and one user then each group manager approve request then only the user will add the group

Is it possible to add the user to the groups via workflow? If yes, Please help me with the script.

Thanks,

1 ACCEPTED SOLUTION

DirkRedeker
Mega Sage

Hi

You need to use an 'Approval - Group' Workflow Activity for the groups to approve.

Inside that 'Approval - Group' Activity you need to select, that the Approval is only valid, when all groups approved.

To send the approval to your desired groups, add the sys_id in the answer array of the script inside the Approval - Group activity.

Let me know if that answered your question and mark my answer as correct and helpful, please.

BR Dirk

View solution in original post

7 REPLIES 7

Hi Dirk thanks for  response. i was  giving to  condition  approval is approved but  its not triggered.

please share script and condition.

 

thanks

chiru

 

 

Hi

Below, you get the results of the scenario, I just created for you to replay...

First, I created a Catalog Item (see screenshot below):

find_real_file.png

1) I added the Item to the Service Catalog, to the Services Category, so that I can order it

2) I added my workflow (see below), to run on this Catalog Item

3) I added the variables, like you described in your question.

 

 

The demo workflow I created looks like that (I added this workflow to the Catalog Item, holding the both variables):

find_real_file.png

For demonstration purposes, I just made a very simple workflow only containing, what is needed for this scenario.

1) An "Approval - Group" Workflow Activity, which will hold a Script to add the Groups from the List Collector in the Requested Item (from Catalog Item)

2) A "Run Script" Workflow Activity, which will script to add the user to the groups (if the Approval was successful).

 

The Script ofg the "Approval - Group" WOrkflow Activity looks like that:

 

// Set the variable 'answer' to a comma-separated list of group ids or an array of group ids to add as approvers.
//
// For example:
//       var answer = [];
//       answer.push('id1');
//       answer.push('id2');


// Copy the Sys_Ids from the List-Collector to the "answer" variable, making them the groups
// needed to approve, before exit condition

workflow.info('Sample logging of the Cat Item Variables:');
workflow.info('Groups sys_ids = "' + current.variables.groups_to_add + '"');
workflow.info('Groups sys_ids = "' + current.variables.groups_to_add.getValue() + '"');
workflow.info('User sys_id = "' + current.variables.user_to_add + '"');
workflow.info('User sys_id = "' + current.variables.user_to_add.getValue() + '"');

answer = current.variables.groups_to_add.getValue(); //  The List collector Values are comma-separated !

 

find_real_file.png

 

The Script for the "Run Script" Workflow Activity looks like that:

var myUsrSysID = "46d44a23a9fe19810012d100cca80666";

var myGrpSysIDs = "74ad1ff3c611227d01d25feac2af603f,8a5055c9c61122780043563ef53438e3,12a586cd0bb23200ecfd818393673a30,5f63e48fc0a8010e00eeaad81cd4dd37,5f6441efc0a8010e0177fcb589156352,5f721d93c0a8010e015533746de18bf9,dc0db135c332010016194ffe5bba8f23,287ebd7da9fe198100f92cc8d1d2154e";

myUsrSysIDs = current.variables.user_to_add.getValue();
myGrpSysIDs = current.variables.groups_to_add.getValue();

workflow.info(myGrpSysIDs);

// Make Array from the comma separated string of Group SysIDs
var arrGrps = myGrpSysIDs.split(',');

workflow.info(arrGrps.length);
workflow.info(arrGrps[0]);

// loop the groups, and check, if the user needs to be added to the group
var i           = 0;
var aktGrpSysID = '';

var grGrp     = new GlideRecord('sys_user_grmember');
var grGrp2Usr = new GlideRecord('sys_user_grmember');

for (i = 0; i < arrGrps.length; i++) {
    // Group SysID for this loop
    aktGrpSysID = arrGrps[i];

    // Check if the user is already in the group
    grGrp = new GlideRecord('sys_user_grmember');
    grGrp.addQuery('user', myUsrSysID);
    grGrp.addQuery('group', aktGrpSysID);
    grGrp.query();

    if (grGrp.next()) {
        // The user is already in the group
        // no action required
        workflow.info('User is already in the group ' + aktGrpSysID);
    } else {
        // The user not yet in that group
        // add the user to the group
        workflow.info('Adding User ' + myUsrSysID + 'to the group ' + aktGrpSysID);

        grGrp2Usr = new GlideRecord('sys_user_grmember');
        grGrp2Usr.initialize();
        grGrp2Usr.setValue('user', myUsrSysID);
        grGrp2Usr.setValue('group', aktGrpSysID);
        grGrp2Usr.insert();
    }
  
}

 

find_real_file.png

 

When I now order an Item, the resulting Requested Item looks like that:

find_real_file.png

I selected to add ths User "Beth Anglin" (1), to the Group "Network" (2). You could also add any number of groups, if needed!

Looking at the workflow Context, I can review the log, about what was done by the script:

find_real_file.png

Looking at the table [sys_user_grmember], I can see, that the Group now is linked with the user:

find_real_file.png

 

I hope, this serves as a good starting point for you.

 

 

 

 

 

 

 

Hi

Just for your information, that there will be NO approver added, of there is NO user in a given  group.

For example, the OOB group "field services" does not hold any users by default.

So, if you select that group, NO user can be addressed by the Group Approval, which will make the approval be "Approved" automatically. So make sure, that there are users to approve for each group.

If that solution was helpful and made you happy, you now can make me happy by marking my both last answers as helpful.

Have fun.

BR

Dirk