UI action to assign task in a group member
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2024 05:42 AM
Hi everyone,
at the pressing of a button I need to iterate through the group members so that the manager can assign the task at another member group in a circular and alphabetical manner. 'Circular' means assign automatically and following an alphabetical order. I'm now using an UI action, and i can't solve find the right way using the script section, is there someone that could help me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-01-2024 12:43 AM
Hello @SimoneA ,
PFB,
(function() {
var groupId = 'your_group_id'; // Replace with your group ID
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', groupId);
gr.orderBy('user.name'); // Order by user name for alphabetical order
gr.query();
var members = [];
while (gr.next()) {
members.push(gr.user);
}
var lastAssignedMemberId = ''; // Fetch from custom table or state management
var lastIndex = members.findIndex(member => member.sys_id.toString() === lastAssignedMemberId);
// Calculate the next index in a circular manner
var nextIndex = (lastIndex + 1) % members.length;
var nextMember = members[nextIndex];
// Assign the task to the next member
var taskRecord = new GlideRecord('task'); // Specify your task table
if (taskRecord.get('sys_id', 'your_task_id')) { // Replace 'your_task_id' with the task ID
taskRecord.assigned_to = nextMember.sys_id;
taskRecord.update();
}
lastAssignedMemberId = nextMember.sys_id.toString();
gs.addInfoMessage('Task assigned to ' + nextMember.name);
})();
If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.
Regards,
Amitoj Wadhera