- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2022 03:03 AM
Hi, All.
I'm triggering the Business Rule to work if the Name of the record inserted in the Event [sysevent] table is login, but it doesn't work.
The reason for this trigger condition is that if the user logged in to the instance has admin privileges, he wants to send an email notification to the system.
Just in case, log information and script contents are also linked.
Someone please help me.
(function executeRule(current, previous /*null when async*/ ) {
var event_name;
var rol = new GlideRecord('sys_user_role');
rol.addQuery('name', 'admin');
rol.query();
if (rol.next()) {
var hasRole = new GlideRecord('sys_user_has_role');
hasRole.addQuery('user.sys_id', gs.getUser().toString());
hasRole.addQuery('role.sys_id', rol.sys_id);
hasRole.query();
if (hasRole.next()) {
event_name = 'Test.Email.Login';
return event_name;
} else {
return false;
}
} else {
return false;
}
})(current, previous);
Thank you in advance for someone helping me.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2022 03:17 AM
Hi Sato,
As far as I have understood your concern / issue. You want to trigger an email once an admin user logs into the system. Please correct me if I am considering it wrong.
I would suggest to change your approach of doing it. Write a BR on user table to check last login time or it changes. and then trigger an event that you will create in registry followed by an email notification that is triggered by event.
I have tested it and can see that it works, refer the below: -
Business Rule:
Table: User [sys_user]
When: after
Order: 100
Active: true
Script:
if (!current.last_login_time.nil() && current.last_login_time.changes() && current.user_name == 'its_admin') {
gs.eventQueue("admin.login", current, current.user_name , current.user_name);
}
Note: - you can explicitly call your event here as well and pass the parameters.
Event Created in registry: -
Name: admin.login
Table: User [sys_user]
Fired By: BR name
Description: Admin User Login Event
Email Notification: -
That will run on Event triggered which you will create above.
Hope this helps.
If my answer resolves your issue, please mark my answer as ✅ Correct & Helpful based on the validations.
Thank You!
Regards,
Kailash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2022 03:17 AM
Hi Sato,
As far as I have understood your concern / issue. You want to trigger an email once an admin user logs into the system. Please correct me if I am considering it wrong.
I would suggest to change your approach of doing it. Write a BR on user table to check last login time or it changes. and then trigger an event that you will create in registry followed by an email notification that is triggered by event.
I have tested it and can see that it works, refer the below: -
Business Rule:
Table: User [sys_user]
When: after
Order: 100
Active: true
Script:
if (!current.last_login_time.nil() && current.last_login_time.changes() && current.user_name == 'its_admin') {
gs.eventQueue("admin.login", current, current.user_name , current.user_name);
}
Note: - you can explicitly call your event here as well and pass the parameters.
Event Created in registry: -
Name: admin.login
Table: User [sys_user]
Fired By: BR name
Description: Admin User Login Event
Email Notification: -
That will run on Event triggered which you will create above.
Hope this helps.
If my answer resolves your issue, please mark my answer as ✅ Correct & Helpful based on the validations.
Thank You!
Regards,
Kailash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2022 03:56 AM
Hi, Mr.
As far as I have understood your concern / issue. You want to trigger an email once an admin user logs into the system. Please correct me if I am considering it wrong.
The recognition of the above contents is correct.
I'm getting an error at [& amp] and can't run the script. Any advice?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2022 04:08 AM
Replace the [@amp] with &. The script automatically converted it. This would be
(!current.last_login_time.nil() && current.last_login_time.changes() && current.user_name == 'its_admin')
Thanks,
Haris
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2022 04:30 AM