GlideRecord - Loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2019 05:50 AM
I have a script where I want to query the members of a group:
var members = new GlideRecord("sys_user_grmember");
members.addQuery('group', '17e3f398db76774082823638fc96196e');
members.query();
var count = members.getRowCount();
var currentCount = 0;
while(members.next()){
while (currentCount < count){
workflow.scratchpad.member_id = members.user.sys_id;
currentCount++;
}
}
The bold is where I get iffy. I want to save the sys_id of the user record, but I want as many workflow variables as would be required to get through all group members. I am having trouble with this part.
The end goal is to use these variables to supply the assigned to for the future catalog tasks that have to be created for the process. Each member of the group has to have a catalog task assigned to them. If anyone has any counter thoughts or different ways of handling this, I am all ears!
Thank you!
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2019 05:55 AM
Hi Asley
var members = new GlideRecord("sys_user_grmember");
members.addQuery('group', '17e3f398db76774082823638fc96196e');
members.query();
var array = [];
var count = 0;
while(members.next()){
array[count] = members.user.sys_id;
count++;
}
workflow.scratchpad.member_id_array = array;
If my answer helped you in any way, please then mark it correct and helpful.
Kind regards,
Manas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2019 05:59 AM
Hi,
store the individual users into an array and store that array in workflow scratchpad
update script as below
var user = [];
var members = new GlideRecord("sys_user_grmember");
members.addQuery('group', '17e3f398db76774082823638fc96196e');
members.query();
var count = members.getRowCount();
var currentCount = 0;
while(members.next()){
user.push(members.getValue('user'));
}
workflow.scratchpad.member_id = arr;
you can use this workflow scratchpad in another run script to create as many catalog tasks and set the assigned to with individual array value
var arr = workflow.scratchpad.member_id.toString().split(',');
for(var i=0;i<arr.length;i++){
var gr = new GlideRecord('sc_task');
gr.initialize();
gr.assigned_to = arr[i];
gr.short_description = ''; // as per your need
gr.insert();
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2019 06:49 AM
The way your script is working you are overwriting the workflow.scratchpad.member_id on each loop.
You need to concatenate it i.e.
workflow.scratchpad.member_id = workflow.scratchpad.member_id + members.user.sys_id + ',';
Then on the receiving end get all the sys_ids into an array using split and loop around the array
Regards
Paul