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

Sato3
Tera Contributor

Hi, Haris.

Sorry, the script worked here.



Script:

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

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

})(current, previous);

 

But there is one problem. It was executed only when newly created with "New button" from the Event [sysevent] table.

The ideal form is when the Admin permission holder logs in. What's wrong.....???

 

 

In this script, the notification was not issued even from the creation of a new record.

Script:

(function executeRule(current, previous /*null when async*/ ) {
	// Add your code here
    // Add your code here
    var grRole = new GlideRecord('sys_user_has_role');
	
	gs.info('Who1!' + current.caller.sys_id);//undfinde
	gs.info('Who3!' + gs.getUserName());//admin
    gs.info('Who7!' + gs.getUserDisplayName());//System Administrator
	
    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);

find_real_file.png

Upender Kumar
Mega Sage

If you are validating in BR then use below

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

If you are trying via script include then write below in your function and call this function in your BR or where you want and check if the return value is not blank.

if(gs.getUser().hasRole('admin'){
 return 'Test.Email.Login';
}
else
 return "";

Ravi9
ServiceNow Employee
ServiceNow Employee

can i suggest something different altogether ? 

go here in ur instance , if you dont know what script action is read about it from docs site !

https://XXX.service-now.com/nav_to.do?uri=sysevent_script_action.do?sys_id=8e93ed730a05001301dec5cd65369e9e

//
// Set the Last login time field on the user record, DO NOT update sys_updated_on, sys_updated_by, or sys_mod_count
//
var gr = new GlideRecord("sys_user");
gr.addQuery("user_name", event.parm1);
gr.query();
if (gr.next()) {
    gr.last_login_time = event.sys_created_on;
    gr.last_login_device = event.parm2;
    gr.autoSysFields(false);
    gr.update();
// ABOVE IS EXISTING CODE - ADD YOURS BELOW
    if (gs.getUser().hasRole("admin"))
        gs.eventQueue(event_name, "glide record object of the same table as your notification");
}

 

Kailash Bhange
Kilo Sage
Kilo Sage

Hi Sato,

 

There is a simple Solution as well!

You can skip the BR, Event part and directly write a notification on user table as below.

 

find_real_file.png

find_real_file.png

find_real_file.png

find_real_file.png

 

Email Logs as below: -

 

find_real_file.png

 

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, @Kailash Bhange .

 

Notifications will occur even for users who do not have admin privileges.

Is it possible to notify only users who have admin rights?