set assignment group in RITM using script in workflow

alrafay182
Tera Contributor

i have created a list collector variable in a catalog item. the list collector contains locations data. i want that when user orders the catalog item, an assignment group is assigned to the RITM based on the count of locations ordered by the user. i want to write script in the workflow to execute this logic. 

i wrote this script but it is not setting assignment group. 

 

 
var approval = new GlideRecord('sysapproval_approver');
approval.approver_group = 'HR agent';
approval.sysapproval = current.sys_id;
approval.insert();

var approval1 = new GlideRecord('sysapproval_approver');
approval1.approver_group = 'HR head';
approval1.sysapproval = current.sys_id;
approval1.insert();

// Processing the locations from the variable
var locations = current.variables.select_location.split(";").map(l => l.trim()).filter(Boolean);
gs.info('Locations Array: ' + locations.join(', '));

var groupId = ''; // Variable to store the group ID to be assigned

if (locations.length == 1) {

    var group = new GlideRecord('sys_user_group');
    group.addQuery('name', 'HR agent');
    group.query();
   
    if (group.next()) {
        groupId = group.sys_id;
        gs.info('Single location group found: ' + group.name);
    } else {
        gs.info('No matching group found for single location');
    }

    current.assignment_group = groupId;
    current.setValue('approval_group', groupId);

    gs.info('Assigned Single Location Group');
} else if (locations.length > 1) {
    var group1 = new GlideRecord('sys_user_group');
    group1.addQuery('name', 'HR head');
    group1.query();

    if (group1.next()) {
        groupId = group1.sys_id;
        gs.info('Multiple location group found: ' + group1.name);
    } else {
        gs.addInfoMessage('No matching group found for multiple locations');
    }

    current.assignment_group = groupId;
    current.setValue('approval_group', groupId);

    gs.info('Assigned Multiple Location Group');
} else {
    gs.addInfoMessage('No locations found.');
}

current.update();
 
please tell me about the corrections required here.
2 REPLIES 2

jcmings
Mega Sage

One thing that jumps out to me right off the bat is this line:

approval.approver_group = 'HR agent';

This should be a sys_id and not the words 'HR agent'

 

 

I would recommend pasting your entire script into Background Scripts and going line-by-line to figure out where it is failing.

Ankur Bawiskar
Tera Patron
Tera Patron

@alrafay182 

if this is written in workflow run script then script is fine.

are you sure the group name used in Query is correct i.e. HR agent and HR head

Also update line as this -> split should be with comma , and not semicolon

var locations = current.variables.select_location.split(",");
gs.info('Locations Array: ' + locations);

 If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader