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

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

Thank you @Harneet Sital It worked.

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.

 

nowdeveloper_0-1719880138742.pngnowdeveloper_1-1719880172972.png

nowdeveloper_2-1719880185602.png

 

Any help would be greatly appreciated.

 

Thanks

There is an easy way to do this.

We did here. I'm replying this on my phone, will send the details tomorrow.