The Now Platform® Washington DC release is live. Watch now!
‎11-14-2013 11:58 AM
Created a new u_user_login table that wanting to track everytime a user logs in. I need to run reports to see how many times a person/dept is logging inot the instance.
I created a business rule on the new table with the following settings:
When:after
Insert:yes
Active:yes
Script
var gr = new GlideRecord("u_user_login");
if (current.operation() == 'login')
{
gr.sys_created_by = event.parm1;
gr.sys_created_on = event.parm2;
}
I used the default columns that were created with the table as I just need to track the user and the date.
I am not sure if I am using the current.operation correctly and I am not seeing a record getting inserted into the new table.
‎11-14-2013 12:01 PM
Correction: I put the Business Rule on the sysevent table not the new table i created.
‎11-14-2013 12:58 PM
current.operation() may return "update" or "insert".
You have to change the if to
if (current.operation() == 'insert' && current.name=='login')
and dont forget to initialize the GlideRecords and use insert().
Also, do not you the fields sys_created_by and sys_created_on, as they are used by the system and set automatically.
Create new fields instead, f.e.
if (current.operation() == 'insert' && current.name=='login')
var gr = new GlideRecord("u_user_login");
gr.initialize();
gr.u_user = event.parm1
gr.u_logged_in_at = current.parm1; // current because the rule runs on Event table
gr.insert();
}
Refer to https://wiki.servicenow.com/index.php?title=GlideRecord for more details
But be aware of this concept, because this would be executed every time the event is inserted. There may be veeery much events a day.
‎11-15-2013 04:33 PM
Here's how I would do this. There is already a "login" event that gets generated when a user logs in, and you can react to a specific event with a new Script Action you create. System Policy > Script Actions.
Parm 1 of the event is the user name, Parm 2 is the IP address.
Create a new Script Action that reacts to the "login" event. No condition. Make it active. Use a variant of the following in the script:
var userLogin = new GlideRecord("u_user_login");
userLogin.u_user_name = event.parm1; // works if u_user_name is just a String field to store User name
userLogin.u_user = event.user_id; // works if u_user is a reference field to the sys_user table
userLogin.u_when = event.sys_created_on; // works if u_when is a date/time field
userLogin.u_ip = event.parm2; // works if u_ip is a String field to store IP address
userLogin.insert();
‎11-18-2013 01:41 PM
I was able to get this option to work. thank you