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

Well the Timer idea didn't work


Have you published your workflow? I sometimes find that things don't work right unless I do. Have you validated that the sys_id that you are storing is one of the groups you actually created? Have not used the getUniqueValue() method, so not sure what it is grabbing. I typically use the following to grab the sys_id:



      var sysID = newgroup.insert();


      workflow.scratchpad.GroupID = sysID;



Otherwise, we have all of the pieces in place. See if you can add some logging to the script include to make sure we are running things correctly:



      addToGroup: function(usrID, grpID) {


gs.log('addToGroup usrID: ' + usrID + ' grpID: ' + grpID);


              var grmember = new GlideRecord('sys_user_grmember');


              grmember.group = grpID;


              grmember.user = usrID;


              grmember.insert();


      },



      chkForMembership: function(usrID, grpID) {


gs.log('chkForMembership usrID: ' + usrID + ' grpID: ' + grpID);


gs.log('chkForMembership return: ' + gs.getUser().getUserByID(usrID).isMemberOf(grpID));


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


      },



You can always remove these when you get things working right.



Let us know what you find.


usrID is a common naming convention that I use to differentiate a user object usrObj from a single sys_id value usrID. In the Run Script you are going through each of the sys_id values stored in the usrArray which is why you use usrArray[u]. If you want to add a different assignment variable for clarity, you can use the following:



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 usrID = usrArray[u]; //the single sys_id slice from the array


      var chkM = sInc.chkForMembership(usrID, grpID);


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


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


      }


}



Both will work.



I believe you are correct in that you may have to wait for the world to catch up before adding the members.


Not much luck with the logs i'm afraid.



For the purpose of testing - im going to try this without the script include.



The issue is with the list collector array. If I use the following code, one sys_user_grmember record is created with the correct group but with a blank user field:



var usrArray = current.variables.bservice_bowners.split(',');


{


      var gr = new GlideRecord('sys_user_grmember');


      gr.initialize();


      gr.group.setDisplayValue(workflow.scratchpad.GroupID);


      gr.user = usrArray;


      gr.insert();


}



If I add in the 2nd line here:



var usrArray = current.variables.bservice_bowners.split(',');


for (u = 0; u < usrArray.length; u++)


{


      var gr = new GlideRecord('sys_user_grmember');


      gr.initialize();


      gr.group.setDisplayValue(workflow.scratchpad.GroupID);


      gr.user = usrArray;


      gr.insert();


}



I gave also tried this with the same result:



var usrArray = current.variables.bservice_bowners.split(',').forEach(function(sys_id)


{


      var gr = new GlideRecord('sys_user_grmember');


      gr.initialize();


      gr.group.setDisplayValue(workflow.scratchpad.GroupID);


      gr.user = usrArray;


      gr.insert();


}



No matter what I try, it wont work. The logs are showing that the sys_id's of the users are being returned from the variable but I cant seem to create a new sys_user_grmember record for each one.


Looking at your code, the reason why the first succeeded, is that it pulled the first slice. The reason why the second two did not work is that you are not indicating which slice of the array you are using for your assignment. Taking your code from the second example, I have modified it to use the slice of the array represented by [u]:



var usrArray = current.variables.bservice_bowners.split(',');  


for (u = 0; u < usrArray.length; u++)  


{  


      var gr = new GlideRecord('sys_user_grmember');    


      gr.initialize();    


      gr.group.setDisplayValue(workflow.scratchpad.GroupID);  


      gr.user = usrArray[u]; //using the slice of the array [u]


      gr.insert();    


}



That should now work.