[object Object] error in notification.Values not displaying correctly in the Email.(License Removal)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-15-2025 01:58 PM
Hi,
Below is the scheduled job script written to remove users from support groups not using itil license.
// Define groups to exclude
var excludedgrps = ['CMDB and ITSM Readers', 'Liberty Mutual Business Stakeholders'];
// Query sys_user_has_role for inactive users that have the itil role, not VIP, not web service access
// If a user never logged in OR not logged in in the last 30 days, not added in the role in the last 7 days
var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role.name=itil^user.active=true^user.vip=false^user.web_service_access_only=false^user.last_login_timeISEMPTY^ORuser.last_login_time<javascript:gs.beginningOfLast30Days()^sys_created_on<javascript:gs.beginningOfLast7Days()');
gr.query();
gs.info('RIU - Query executed. Number of Users found: ' + gr.getRowCount());
var removedUsers = [];
while (gr.next()) {
// Find group membership for the user where the group is of type Support
var groupGr = new GlideRecord('sys_user_grmember');
groupGr.addEncodedQuery('group.typeCONTAINS1cb8ab9bff500200158bffffffffff62^user=' + gr.user);
groupGr.query();
gs.info('RIU - Query executed. Users to be removed: ' + groupGr.getRowCount());
while (groupGr.next()) {
var groupName = groupGr.group.getDisplayValue();
// Check if the user belongs to the excluded group
if (excludedgrps.indexOf(groupName) == -1) {
gs.info('Removing user from group: ' + gr.user + ' (Sys ID: ' + gr.sys_id + ') from group: ' + groupName);
removedUsers.push(gr.name + ' (Sys ID: ' + gr.sys_id + ') from group: ' + groupName);
groupGr.deleteRecord(); // Remove from the group
}
}
}
gs.info('RIU - Script execution Completed');
// Trigger event if users were removed
if (removedUsers.length > 0) {
// Prepare the event parameters
var eventParams = {
parm1 : removedUsers.join('\n'),
};
// Trigger the event for the notification
gs.eventQueue('inactive_users_removed_event', gr, eventParams);
gs.info('RIU - Event triggered for inactive users removed');
} else {
gs.info('RIU - No users were removed');
}
I have created an event - inactive_users_removed_event (on sys_user_has role table) and an email notification (on sys_user_has_role table)
Below is the Email script written.
var removedUsers = event.parm1 || ''; // 'removed_users' passed in event param
var timeStamp = new GlideDateTime().getDisplayValue();
template.print('<p>The following inactive users have been removed from their assigned support groups:</p>');
template.print('<p><strong>Removed Users:</strong></p>');
template.print('<p>' + removedUsers + '</p>');
template.print('<p>This action was taken on <strong>' + timeStamp + '</strong> because these users:</p>');
template.print('<ul>' +
'<li>Have not logged in at all</li>' +
'<li>Have not logged in within the past 30 days</li>' +
'<li>Or were not recently (within the last 7 days) assigned their role</li>' +
'</ul>');
template.print('<p>Please refer to the system logs for additional details.</p>');
The users are removed, email is getting created, I can see in the logs as well. But I want the list of users removed to the groups to be added in the email, which is not working.
timestamp is working fine but Removed Users is not working.
In the email, its like Removed Users : [object Object] is populating.
If anyone can help me on this please?
Thanks & Regards,
Vaishnavi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-15-2025 09:45 PM
try this
Scheduled job
// Define groups to exclude
var excludedgrps = ['CMDB and ITSM Readers', 'Liberty Mutual Business Stakeholders'];
// Query sys_user_has_role for inactive users that have the itil role, not VIP, not web service access
// If a user never logged in OR not logged in in the last 30 days, not added in the role in the last 7 days
var gr = new GlideRecord('sys_user_has_role');
gr.addEncodedQuery('role.name=itil^user.active=true^user.vip=false^user.web_service_access_only=false^user.last_login_timeISEMPTY^ORuser.last_login_time<javascript:gs.beginningOfLast30Days()^sys_created_on<javascript:gs.beginningOfLast7Days()');
gr.query();
gs.info('RIU - Query executed. Number of Users found: ' + gr.getRowCount());
var removedUsers = [];
while (gr.next()) {
// Find group membership for the user where the group is of type Support
var groupGr = new GlideRecord('sys_user_grmember');
groupGr.addEncodedQuery('group.typeCONTAINS1cb8ab9bff500200158bffffffffff62^user=' + gr.user);
groupGr.query();
gs.info('RIU - Query executed. Users to be removed: ' + groupGr.getRowCount());
while (groupGr.next()) {
var groupName = groupGr.group.getDisplayValue();
// Check if the user belongs to the excluded group
if (excludedgrps.indexOf(groupName) == -1) {
gs.info('Removing user from group: ' + gr.user + ' (Sys ID: ' + gr.sys_id + ') from group: ' + groupName);
removedUsers.push(gr.user.getDisplayValue() + ' (Sys ID: ' + gr.sys_id + ') from group: ' + groupName);
groupGr.deleteRecord(); // Remove from the group
}
}
}
gs.info('RIU - Script execution Completed');
// Trigger event if users were removed
if (removedUsers.length > 0) {
// Prepare the event parameters
var eventParams = {
parm1: removedUsers.join('\n')
};
// Trigger the event for the notification
gs.eventQueue('inactive_users_removed_event', gr, eventParams);
gs.info('RIU - Event triggered for inactive users removed');
} else {
gs.info('RIU - No users were removed');
}
Email script:
var removedUsers = event.parm1 || ''; // 'removed_users' passed in event param
var timeStamp = new GlideDateTime().getDisplayValue();
template.print('<p>The following inactive users have been removed from their assigned support groups:</p>');
template.print('<p><strong>Removed Users:</strong></p>');
template.print('<p>' + removedUsers + '</p>');
template.print('<p>This action was taken on <strong>' + timeStamp + '</strong> because these users:</p>');
template.print('<ul>' +
'<li>Have not logged in at all</li>' +
'<li>Have not logged in within the past 30 days</li>' +
'<li>Or were not recently (within the last 7 days) assigned their role</li>' +
'</ul>');
template.print('<p>Please refer to the system logs for additional details.</p>');
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2025 05:10 AM
Hi Ankur,
I am still getting the same error.
Removed Users :
[object Object]
Thanks,
Vaishnavi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2025 05:11 AM
Thank you for marking my response as helpful.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-16-2025 06:43 AM
It did not work Ankur, I am still getting the same error. [object Object] values are not populating.
what should be the table for notification? sys_user_grmember or sys_user or sys_user_has_role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-17-2025 05:02 AM
table -> sys_user_has_role
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader