Business Rule triggers don't work.

Sato3
Tera Contributor

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.

1 ACCEPTED SOLUTION

Kailash Bhange
Kilo Sage
Kilo Sage

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

View solution in original post

16 REPLIES 16

Hi Sato, There is script validation added for admin role as well. Pls refer screenshot.

Hi, @Kailash Bhange .

 

I'm testing with the screenshots provided, but I'm also notified to users who don't have administrator privileges.

 

What is causing the problem?

Is it a problem with my instance itself?