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,

the BR on user table should run on insert, update.. so that whenever there is change in the login time it will trigger the code.

and from code you are invoking the event. once the event is triggered, there should be notification which will trigger and send email.

 

Hope this helps.
If my answer resolves your issue, please mark my answer as ✅ Correct & Helpful based on the validations.

Thank You!
Regards,
Kailash

Hi, @Aarti Kachale .

 

When I verified it again, it matched the condition of
"" 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. ""
It was the only one.

 

What didn't work was my lack of understanding. I'm sorry.

Thank you for your support and information sharing.

Haris5
Giga Contributor

Hi Sato,

When you return from a Business Rule it will not work. You can trigger the event from the Business Rule using the below script. Also there is no need to GlideRecord multiple tables you can check it with gs.getUser.hasRole('admin').

if(gs.getUser().hasRole('admin'){
gs.eventQueue('Test.Email.Login', current, gs.getUserID(), gs.getUserName());
}

 

Thanks,

Haris

Sato3
Tera Contributor

Hi, Haris.

It didn't work. It seems that the Business Rule isn't working, rather than a problem with the script.


Event registry, Notification, Business Rule,
All target tables are Event [sysevent].

What information should I give you to resolve it?

Haris5
Giga Contributor

Hi Sato,

Got your requirements now. I changed the BR and this is working for me now. Please check if this works

find_real_file.png

 

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var grRole = new GlideRecord('sys_user_has_role');
    grRole.addQuery('user', current.user_id);
	grRole.addQuery('role=2831a114c611228501d4ea6c309d626d');//admin role sys_id
	grRole.setLimit(1);
	grRole.query();
	if(grRole.next()){
		gs.eventQueue('Test.Email.Login', current, gs.getUserID(), gs.getUserName());
	}




})(current, previous);

 

Please let me know if this still doesn't work out. Also instead of checking the logs you need to check the notification itself if it triggered.

 

Thanks,

Haris