Email notification on local admin login in Prod Environment

now developer
Tera Contributor

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.

 

  1. Instance Security center - tried looking for notifications which were mentioned in the previous threads - Unable to locate them in preferences from profile.
  2. Tried the same script that is suggested, but what is the event that needs to be triggered?
  3. Mail script for content of the email?

b__DeepthiR_0-1718651655917.pngb__DeepthiR_1-1718651678590.png

 

Please help.

 

Thanks

1 ACCEPTED SOLUTION

Harneet Sital
Mega Sage
Mega Sage

Hi @now developer 

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

HarneetSital_0-1718969289988.png

2. Update the when to run conditions as below 

 

HarneetSital_1-1718969361173.png

 

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

View solution in original post

10 REPLIES 10

Hi @now developer the setup looks good, can you check if the individuals who you have added as users have an email id? Try adding your personal id or user account and see if the email is sent out. 

Secondly, are the email notifications enabled on your platform? You can check these email properties. 

 

Let me know and we can try other options if these don't work. 

Can you please check in the preference settings, if enable notification is enabled for the users for which you are testing the notification.

Please Mark my answer as helpful if it helped you in resolving the issue 

Hi, I'm having issue where the above is not triggering the notification. Can someone please explain what does the following part do inside the advanced condition: 

// Example usage
var isAdmin = userHasAdminRole(userId);
answer = isAdmin;

-O-
Kilo Patron
Kilo Patron

Another solution to this is to create the notification for insert on table sn_vsc_login_event where all admin logins are clearly marked.

You would no longer need advanced conditions and all that script.

rahelamar
Tera Contributor

This is brilliant! Thanks a lot!