Populate assignment group using assignment rule.

ginasp
Tera Contributor

We have a requirement stating that if an Incident is created from an Interaction, then default assignment group should be based on the following logic:

  • If logged-in/opened by user is part of any ITIL group then we have to update that group to the assignment group on incident

  • If the logged-in/opened by user belongs to multiple ITIL groups, the group that appears first alphabetically should be assigned.

Below is the script which I tried but not working. Could someone please help me.

// Run only when assignment_group is empty
if (current.assignment_group)
return;

// Get the user from opened_by
var user = current.opened_by ? current.opened_by.toString() : gs.getUserID();

// Check if an Interaction is linked to this Incident
var grInteraction = new GlideRecord('interaction');
grInteraction.addQuery('task', current.sys_id);
grInteraction.setLimit(1);
grInteraction.query();

// If no interaction yet, don’t exit — link may not exist yet on insert
// Instead, allow assignment logic to continue only for Interaction-created Incidents
var hasInteraction = grInteraction.next();

// Proceed only if record is from an Interaction OR opened_by exists
if (!hasInteraction && !user)
return;

// Collect all active groups for that user
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', user);
grMember.addQuery('group.active', true);
grMember.query();

var groupList = [];
while (grMember.next()) {
groupList.push(grMember.group.toString());
}

if (groupList.length == 0)
return;

// Select the first alphabetical group (and type if needed)
var grGroup = new GlideRecord('sys_user_group');
grGroup.addQuery('sys_id', 'IN', groupList.join(','));
grGroup.addQuery('type', '1cb8ab9bff500200158bffffffffff62'); // your Helpdesk type sys_id
grGroup.orderBy('name');
grGroup.setLimit(1);
grGroup.query();

if (grGroup.next()) {
current.assignment_group = grGroup.sys_id;
}


ginasp_0-1761683981127.png

@Ankur Bawiskar 

@Dr Atul G- LNG 

@SANDEEP DUTTA 

@SumanthDosapati 

3 REPLIES 3

J Siva
Kilo Patron
Kilo Patron

Hi @ginasp 
Try the below configuration.
We have a OOB field called "Ori gin Table". which can be used to check if the incident is created from the interaction record.

JSiva_0-1761735704729.png

Script:

var group_id_array = [];
var user_id =gs.getUserID();
var group_record = new GlideRecord('sys_user_grmember');
group_record.addEncodedQuery('group.roles=ITIL^user=' + user_id);
group_record.query();
while (group_record.next()) {
    group_id_array.push(group_record.group.name);
}
if (group_id_array.length === 1) {
    current.assignment_group.setDisplayValue(group_id_array[0]);
} else {
    group_id_array.sort();
     current.assignment_group.setDisplayValue(group_id_array[0]);
}

Regards,
Siva

ginasp
Tera Contributor

Hi @J Siva ,

 

Thanks for the response.

 

I have tried the solution which you have provided but it's not working.

Anupam1
Mega Guru

// Run only when assignment_group is empty
if (!current.assignment_group) {

// Get the user from opened_by or fallback to logged-in user
var userID = current.opened_by ? current.opened_by.sys_id.toString() : gs.getUserID();

// Check if an Interaction is linked to this Incident
var grInteraction = new GlideRecord('interaction');
grInteraction.addQuery('task', current.sys_id);
grInteraction.query();

var hasInteraction = grInteraction.hasNext();

// Proceed only if record is from an Interaction OR opened_by exists
if (hasInteraction || userID) {

// Collect all active groups for that user
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('user', userID);
grMember.addQuery('group.active', true);
grMember.query();

var groupList = [];
while (grMember.next()) {
groupList.push(grMember.group.sys_id.toString());
}

if (groupList.length > 0) {
// Select the first alphabetical ITIL group
var grGroup = new GlideRecord('sys_user_group');
grGroup.addQuery('sys_id', 'IN', groupList);
grGroup.addQuery('roles.name', 'ITIL'); // Filter by ITIL role
grGroup.orderBy('name');
grGroup.setLimit(1);
grGroup.query();

if (grGroup.next()) {
current.assignment_group = grGroup.sys_id;
}
}
}
}

Key Fixes and Enhancements:-

1. Corrected group.sys_id access: Use grMember.group.sys_id.toString() instead of group.toString() to ensure you're getting the actual sys_id.

2. Used roles.name instead of type: Filtering by role name (ITIL) is more reliable than using a custom group type sys_id.

3. Used hasNext(): More robust than next() when checking for existence without advancing the record.

Guarded against null opened_by: Ensures fallback to gs.getUserID() is safe.