- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello,
I need to trigger an email when a user with admin access login to prod. I wrote a After BR on Insert and update on User Role[sys_user_has_role] table-> created a event and a notification. But the BR is not executing and event is not triggering.
Is there any other approach or better way to do it?
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @swathi55 ,
The reason your current approach isn't working is that the sys_user_has_role table only tracks changes to a user's assigned roles, not when they actually log into the system.
You can use this approach :
1) Create a Script Action : Navigate to System Policy > Events > Script Actions and click New.
2) Create your Custom Event : Navigate to System Policy > Events > Registry and click New.
3) Create Notification : Navigate to System Notification > Email > Notifications.
- Table : User (sys_user)
- Send when: Event is fired
- Event name: <event_name>
So as a result, whenever the user logged in user will get the notification :
If my response helped mark as helpful and accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
hi @swathi55 ,
The sys_user_has_role table is updated when roles are assigned or removed, not when a user logs in. That's why your Business Rule isn't triggered during login.
I would suggest using a login-triggered mechanism, such as a Login Event or a Business Rule on the appropriate login-related event, to check whether the logged-in user has the admin role. If they do, queue an event and trigger the email notification.
Make sure you test this carefully in a non-production instance first to avoid generating unnecessary emails.
If this helped, mark it helpful and accept the solution.
Regards, Tejas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @swathi55 ,
Why the BR isn’t firing: sys_user_has_role is only written when a role is granted or revoked, not when a user logs in, so an Insert/Update BR on it will never run at login.
Solution: Script Action on the login event
For this event, event.parm1 = user name and event.parm2 = login device.
- Register a custom event custom.admin.login (table: sys_user) in System Policy > Events > Registry.
- Create a Script Action in System Policy > Events > Script Actions (leave the OOB one alone):
- Event name: login
- Active: true
- Script:
var userGr = new GlideRecord('sys_user');
userGr.addQuery('user_name', event.parm1);
userGr.setLimit(1);
userGr.query();
if (userGr.next()) {
var roleGr = new GlideRecord('sys_user_has_role');
roleGr.addQuery('user', userGr.getUniqueValue());
roleGr.addQuery('role.name', 'admin');
roleGr.addQuery('state', 'active');
roleGr.setLimit(1);
roleGr.query();
if (roleGr.hasNext()) {
gs.eventQueue('custom.admin.login', userGr, userGr.getValue('user_name'), event.parm2);
}
}- Set your Notification to trigger on custom.admin.login. Use ${event.parm1} and ${event.parm2} in the body.
sys_user_has_role includes inherited roles, so indirect admin access is covered. A BR on sys_user isn’t reliable here because the OOB script updates last_login_time with autoSysFields(false).
If it doesn’t work, check System Logs > Events to confirm both login and custom.admin.login are processed.
Mark this as Helpful if it clarifies the issue.
Accept the solution if this answers your question.
Regards,
Vaishnavi
Technical Consultant
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @swathi55
Your Business Rule doesn’t fire because sys_user_has_role only changes when a role is granted, inherited. Nothing is written to that table when someone logs in, so the BR never runs and the event is never queued.
Approach : After Update BR on sys_user , condition when last login time changes.
(function executeRule(current, previous) {
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', current.getUniqueValue());
gr.addQuery('role.name', 'admin');
gr.addQuery('state', 'active');
gr.setLimit( 1 ) ;
gr.query();
if (gr.hasNext()) {
gs.eventQueue('admin.user.login', current, current.getValue('user_name'), current.getDisplayValue());
}
})(current, previous);
If this response is helpful, Accept ✔️ the solution , mark it helpful 👍.
Regards,
Abhishek
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @swathi55 ,
The reason your current approach isn't working is that the sys_user_has_role table only tracks changes to a user's assigned roles, not when they actually log into the system.
You can use this approach :
1) Create a Script Action : Navigate to System Policy > Events > Script Actions and click New.
2) Create your Custom Event : Navigate to System Policy > Events > Registry and click New.
3) Create Notification : Navigate to System Notification > Email > Notifications.
- Table : User (sys_user)
- Send when: Event is fired
- Event name: <event_name>
So as a result, whenever the user logged in user will get the notification :
If my response helped mark as helpful and accept the solution.