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

Subhajit1
Giga Guru

Get the sys_ids of the Users added to the List Collector field into an Array.


Create a Gliderecord Insert on the sys_user_grmember table and push the sys_ids from the Array, one by one, to the user field of the records that you instantiate by your GlideRecord.



The array would be Comma Separated value of sys_ids, so split at ',' when you push the sys_ids one by one.


Thanks Subhajit,



Can you let me know how this would look? I haven't really delved into arrays before - ive only just got my head a around glide records.



Say the list collector variable was called 'selectuser' and this is my code to create the glide record:



var newmember = new GlideRecord('sys_user_grmember');  


newmember.initialize();  


newitem.setDisplayValue('name', 'Group Name');      


newitem.insert();



Sorry for being a bit stupid here.



Thanks


Harry


shouvik
ServiceNow Employee
ServiceNow Employee

Harry,



Do you have a Catalog Item or a Record Producer?


If it is a Catalog Item, then in a BR for RITM, you can access the variables using current.variables.variable_name.


If it is a Record Producer, then you can use Record Producer Script. You can access the List Collector variable by producer.variable_name.



In either way you can use the following script to associate the sys_ids to the group.



var sys_ids = current.variables.variable_name [or producer.variable_name]


var group_id = current.variables.group_id [or producer.group_id]


sys_ids.split(',').forEach(function(sys_id) {


        var gr = new GlideRecord('sys_user_grmember');


        gr.initialize();


        gr.group = group_idgroup_id;


        gr.user = sys_id;


        gr.insert();


});





Thanks


Shouvik


PS: Hit like, Helpful or Correct depending on the impact of the response


This is being done using a catalog item. The group itself is created by the workflow before this step so I have tweaked it slightly but cant get it to work, the sys_id of the new group is added to the scratchpad:



var sys_ids = current.variables.bservice_bowners;


var group_id = workflow.scratchpad.GroupID


sys_ids.split(',').forEach(function(sys_id) {  


        var gr = new GlideRecord('sys_user_grmember');  


        gr.initialize();  


        gr.group = group_id);


        gr.user = sys_ids;  


        gr.insert();  


});  



I have also tried the following but still not working:



var sys_ids = current.variables.bservice_bowners;


sys_ids.split(',').forEach(function(sys_id) {  


        var gr = new GlideRecord('sys_user_grmember');  


        gr.initialize();  


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


        gr.user = sys_ids;  


        gr.insert();  


});  



any ideas where i am going wrong?