Using list collector variables to create glide records

Harry Campbell2
Mega Guru

Hello Everyone.

I have request with a list collector where a user selects users to be added to a group.

What is the best way to take the selected users from the list collector and add them to the selected group?

I'm guessing the best way to do this in a workflow would be to create a new glide record on the sys_user_grmember table   but I cant figure out how to pass all the sys_id's of the selected users into the script that creates the new record.

Hope that makes sense.

Thanks

Harry

29 REPLIES 29

That is correct Christopher.



The list collector itself is called bservice_bowners - I assume there wouldn't be a separate variable to 'store' the list of users?


This may seem a bit too complex, but this solution offers the best way of re-purposing your code as well as keeping your code out of the workflow.



Create a Script Include that you will call from within the workflow. I do this so that you can use these functions in other places as well as in multiple workflows.


Name: groupUtils


Client callable: false


Application: Global


Accessible from: All application scopes


Script:


var groupUtils = Class.create();


groupUtils.prototype = {


      initialize: function() {


      },



      addToGroup: function(usrID, grpID) {


              var grmember = new GlideRecord('sys_user_grmember');


              grmember.group = grpID;


              grmember.user = usrID;


              grmember.insert();


      },



      chkForMembership: function(usrID, grpID) {


              return gs.getUser().getUserByID(usrID).isMemberOf(grpID);


      },



      removeFromGroup: function(usrID, grpID) {


              var grmember = new GlideRecord('sys_user_grmember');


              grmember.addQuery('group', grpID);


              grmember.addQuery('user', usrID);


              grmember.query();


              while (grmember.next()) {


                      grmember.deleteRecord();


              }


      },



      type: 'groupUtils'


};



You may want to add your function for creating the group record into this as well. I will leave it up to you to decide.



Since you have the list of users stored from your list collector called bservice_bowners and the group sys_id from your workflow.scratchpad, you can use those in a Run Scripts workflow activity:



var sInc = new GroupUtils(); //initialize the script include


var usrArray = current.variables.bservice_bowners.split(','); // store the list of users as an array so you can work with them easily.


var grpID = workflow.scratchpad.GroupID; // using a shorter variable name


for (u = 0; u < usrArray.length; u++) { // iterate through each slice of the array doing something with each slice


      var chkM = sInc.chkForMembership(usrArray[u], grpID);


      if (!chkM) { //if user is not a member


              sInc.addToGroup(usrArray[u], grpID); //add user to group


      }


}



Let me know if you have any questions,


Thanks for this, it definitely will be useful to have the scrip include.



Unfortunately it's still not working. The group gets created and the workflow progresses without any errors but nobody is added to the group.


Have you validated that the group you created exists in the Group [sys_user_group] table?



How are you setting the workflow.scratchpad variable. seeing {"GroupID":"f18c8db60f692a002e511efae1050e0a"} makes me wonder how that is being set. See what happens if you add the workflow.scratchpad variable to your logs:



gs.log(workflow.scratchpad.GroupID, 'GroupLog'); //I added 'GroupLog' so that you can quickly find it in the logs.



Also, could you share how your workflow is set up so we understand the order of your flow.


Yes the group does show on the sys_user_group table.



The group is created with the following script, at the bottom you can see where it is added to the scratchpad:



var newgroup = new GlideRecord('sys_user_group');


newgroup.initialize();


newgroup.name = 'CA_BO_' + current.variables.bservice_name;


newgroup.insert();


workflow.scratchpad.GroupID = newgroup.getUniqueValue();



I added it to the log and it returns the sys_id of the created group:



find_real_file.png


This is a view of the workflow:



find_real_file.png



I noticed on the script include, the variable usrID but on the workflow scrip I can see usrID, should these be different?



I'm also thinking it could be a timing issue.....I'm going to add a timer in to wait for 10 seconds before adding the members to the group.