Assignment Groups, Notifying Owners
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-30-2023 02:34 PM
I need to send an assignment group owner a notification when someone in their group changes departments. It seems to be working mostly, right up until the GlideRecord query. Logging, I'm seeing my variables populated, but nothing logs for gs.info("Assignment Groups: " + assignGroup), doesn't even say undefined.
(function executeRule(current, previous /*null when async*/ ) {
// Calls event when assignment group changes
//gs.eventQueue('multco.ass.grp.changes',current,current.user,current.group);
if (gs.hasRole('itil')) {
// logging
gs.info("Assignment Group Change: " + gs.hasRole('itil'));
var userID = current.name;
var userRef = userID.toString();
gs.info("Current User ID: " + userID);
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userID);
gr.query();
while (gr.next()) {
var assignGroup = gr.group.getDisplayValue();
gs.info("Assignment Groups: " + assignGroup);
gs.eventQueue('multco.ass.grp.changes', current, assignGroup);
}
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-30-2023 03:51 PM
Hello @Kolesnick,
- You may want to use the getRefRecord method instead of the getDisplayValue method to get the assignment group record from the reference field. This will allow you to access the fields and methods of the referenced record directly, without having to query it again.
- You may want to add some error handling and logging to your query, such as checking if the query returns any records, and logging the number of records returned and the sys_id of each record. This will help you identify any issues with your query logic or data.
- You may want to use the addActiveQuery method instead of the addQuery method to filter only active assignment groups. This will improve your query performance and accuracy.
Here is an example of how your code could look like after applying these suggestions:
(function executeRule(current, previous) {
// Calls event when assignment group changes
// gs.eventQueue(āmultco.ass.grp.changesā, current, current.user, current.group);
if (gs.hasRole(āitilā)) {
// logging
gs.info("Assignment Group Change: " + gs.hasRole(āitilā));
var userID = current.name; var userRef = userID.toString();
gs.info("Current User ID: " + userID);
var gr = new GlideRecord(āsys_user_grmemberā);
gr.addQuery(āuserā, userID);
gr.addActiveQuery(); // filter only active groups
gr.query();
// check if query returns any records
if (gr.hasNext()) { // log the number of records returned
gs.info("Number of Assignment Groups: " + gr.getRowCount());
while (gr.next()) { // get the assignment group record from the reference field
var assignGroup = gr.group.getRefRecord();
// log the sys_id and name of each group
gs.info("Assignment Group: " + assignGroup.sys_id + " - " + assignGroup.name);
gs.eventQueue(āmultco.ass.grp.changesā, current, assignGroup);
}
} else { // log if no records are returned
gs.info("No Assignment Groups found for user: " + userID);
}
}
})(current, previous);
Hope this helps.
Kind Regards,
Swarnadeep Nandy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-30-2023 04:07 PM
Thank you for your recommendations. When I added your recommended logging, it showed no assignment groups found, despite this user being part of multiple groups and verified with the itil role, which also shows as true with the .hasRole
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā08-30-2023 04:06 PM - edited ā08-30-2023 04:07 PM
Edited to move reply to replier.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-02-2023 12:22 AM - edited ā09-02-2023 12:24 AM
Hi @Kolesnick,
I believe the reason you are not getting any results is because your query is broken.
- I am assuming the following from your post:
You are running this code in a Business Rule on the sys_user table when the department changes (on Update). - You would like to trigger an event that will be used to send a notification.
- You want the notification to be sent to the Manager of the Group for every group that the current user is a member of.
Here is your code:
(function executeRule(current, previous /*null when async*/ ) {
// Calls event when assignment group changes
//gs.eventQueue('multco.ass.grp.changes',current,current.user,current.group);
if (gs.hasRole('itil')) {
// logging
gs.info("Assignment Group Change: " + gs.hasRole('itil'));
var userID = current.name;
var userRef = userID.toString();
gs.info("Current User ID: " + userID);
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', userID);
gr.query();
while (gr.next()) {
var assignGroup = gr.group.getDisplayValue();
gs.info("Assignment Groups: " + assignGroup);
gs.eventQueue('multco.ass.grp.changes', current, assignGroup);
}
}
})(current, previous);
I actually see a few issues with this approach.
First of all, you don't want to run a query using the user's name. On the group member table we only have the Group Sys Id and the User Sys Id, so let's start there.
You need to have the user's sys_id to search for their groups. Then you need to get the manager's sys_id for each of those groups. Finally, for the event-triggered notification, you will want to pass in the following:
- the current record so you can get all the updated values from the variables list to use in the message.
- the assignment group's manager (sys_id) for your recipient
- and if you want to use the previous department name in the message, that would be needed here as well.
I would also move the if condition to the business rule condition box above the script field instead.
Here is what the code will look like:
var userId = current.getUniqueValue(); // user record sys_id
var managerId = ''; // we will need to get this for each group to pass on to our event parm 1
var previousDept = previous.department.getDisplayValue(); // display name of the department for event parm 2
//Next we need to get all the groups our user belongs to
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('user', userId);
groupMember.query();
while (groupMember.next()) {
// for each group we want to get the managers sys_id and trigger the event
managerId = groupMember.group.manager;
gs.eventQueue('multco.ass.grp.changes', current, managerId , previousDept );
}
With your event firing correctly we can now take a look at the notification.
${event.parm1}
${event.parm2}
So your message may look something like this:
The department for user ${name} has been changed from ${event.parm2} to ${department}
Give that a shot and let me know if it works for you.
š