- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
06-17-2024 12:15 PM
Hello,
I have a requirement - when a local admin in PROD environment logs into the instance, a notification should be sent. I tried following the existing threads, but I am not able to trigger the notification. Here are few things I performed before I posted this question here in this forum.
- Instance Security center - tried looking for notifications which were mentioned in the previous threads - Unable to locate them in preferences from profile.
 - Tried the same script that is suggested, but what is the event that needs to be triggered?
 - Mail script for content of the email?
 
Please help.
Thanks
Solved! Go to Solution.
- Labels:
 - 
						
							
		
			Architect
 
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
06-21-2024 04:38 AM
I see on the image you have selected send when 'Event is fired', it should be changed to 'Record inserted or updated', and also the script requires a change. Here is what you can try out -
1. Notification should be on the sys_user table
2. Update the when to run conditions as below
3. Use the below-mentioned script for the advanced condition -
function userHasAdminRole(userId) {
    var gr = new GlideRecord('sys_user_has_role');
    gr.addQuery('user', userId);
    gr.addQuery('role.name', 'admin');
    gr.query();
    // Check if any records were returned
    if (gr.next()) {
        return true;  // User has the admin role
    } else {
        return false; // User does not have the admin role
    }
}
// Example usage
var userId = gs.getUserID();  // currently logged in user
var isAdmin = userHasAdminRole(userId);
answer = isAdmin;
I have tested this and it works so should be straightforward for you.
-Harneet Sital
Request you please mark my answer as helpful or correct based on the impact
Find all my ServiceNow articles here
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
06-21-2024 04:38 AM
I see on the image you have selected send when 'Event is fired', it should be changed to 'Record inserted or updated', and also the script requires a change. Here is what you can try out -
1. Notification should be on the sys_user table
2. Update the when to run conditions as below
3. Use the below-mentioned script for the advanced condition -
function userHasAdminRole(userId) {
    var gr = new GlideRecord('sys_user_has_role');
    gr.addQuery('user', userId);
    gr.addQuery('role.name', 'admin');
    gr.query();
    // Check if any records were returned
    if (gr.next()) {
        return true;  // User has the admin role
    } else {
        return false; // User does not have the admin role
    }
}
// Example usage
var userId = gs.getUserID();  // currently logged in user
var isAdmin = userHasAdminRole(userId);
answer = isAdmin;
I have tested this and it works so should be straightforward for you.
-Harneet Sital
Request you please mark my answer as helpful or correct based on the impact
Find all my ServiceNow articles here
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
06-21-2024 05:51 AM
Thank you @Harneet Sital It worked.
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
07-01-2024 05:30 PM
Hello @Harneet Sital
This script worked just fine in PDI, and now its not triggering any notifications. Can you please help me with this.
I am sharing the screenshots.
Any help would be greatly appreciated.
Thanks
- Mark as New
 - Bookmark
 - Subscribe
 - Mute
 - Subscribe to RSS Feed
 - Permalink
 - Report Inappropriate Content
 
07-01-2024 07:41 PM
There is an easy way to do this.
We did here. I'm replying this on my phone, will send the details tomorrow.
