Retrieving current UserID and UserDisplayName and then adding to database table

newbiedev
Kilo Explorer

Hi,

I am trying to create a log table for my application so that I can log user activity. Apart from the default fields that are available, I would also like to add the user's id and the user's display name to the log table. I have tried using GlideSystem for this purpose. I know that my GlideSystem syntax is correct as I have tested it in the ServiceNow backgroud script. I tried to write a script into a BusinessRule but it didn't work out. To be honest, I am not sure which the best place to even write that script. New to ServiceNow. Here's the script just in case:

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

var clientDataLog = new GlideRecord('<some_table_name_here>');

var currentUser = gs.getUser();

clientDataLog.addQuery();

clientDataLog.query();

clientDataLog.userID = gs.info(currentUser.getUserID());

clientDataLog.username = gs.info(currentUser.getDisplayName());

clientDataLog.update();

})(current, previous);

This business rule gets triggered after a table insert is made by the client script on my UI page. However, there are two problems. One it is making multiple logs/updates to the table for only one trigger. Second, it is wiping out the values inserted by the client script.

Again, I am pretty new to ServiceNow. My bad if what I did was totally stupid

Eagerly waiting for some expert input.

Thanks

D Java

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Hello,



Please find my suggestion below. Please check if it helps.



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


//var currentUser = gs.getUser(); // I don't think this is needed


var clientDataLog = new GlideRecord('<some_table_name_here>'); // Hoping you are doing GlideRecord on your custom log table


clientDataLog.addQuery();


clientDataLog.query();


while(clientDataLog.next()){ //Please use next() after GlideRecord Query


//clientDataLog.userID = gs.info(currentUser.getUserID()); // you can directly add gs to get the user details


//clientDataLog.username = gs.info(currentUser.getDisplayName()); // gs.info() is to log data in system logs


clientDataLog.userID = gs.getUserID();


clientDataLog.username = gs.getUserDisplayName();


clientDataLog.update();


}


})(current, previous);


View solution in original post

4 REPLIES 4

Shishir Srivast
Mega Sage

Hello,



Please find my suggestion below. Please check if it helps.



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


//var currentUser = gs.getUser(); // I don't think this is needed


var clientDataLog = new GlideRecord('<some_table_name_here>'); // Hoping you are doing GlideRecord on your custom log table


clientDataLog.addQuery();


clientDataLog.query();


while(clientDataLog.next()){ //Please use next() after GlideRecord Query


//clientDataLog.userID = gs.info(currentUser.getUserID()); // you can directly add gs to get the user details


//clientDataLog.username = gs.info(currentUser.getDisplayName()); // gs.info() is to log data in system logs


clientDataLog.userID = gs.getUserID();


clientDataLog.username = gs.getUserDisplayName();


clientDataLog.update();


}


})(current, previous);


Thanks Shishir. Your suggestion worked. However, there is one more problem which I would like to fix. As for now, for every transaction the business rule is making multiple logs/updates to the custom log table. I would like it to only make one log for one transaction.



This is how my business rule is set at the moment:



When to run tab:


When- after                       Insert is checked


Order- 1



No filter conditions



Actions tab:


None



Advanced:


The working script you suggested



Let me know if I need to provide more info. Eagerly waiting for your reply



Thanks



D Java


Good news! The issue seemed to have resolved by itself. I don't know what the problem was but now everything works like a charm. Once again, thanks a ton Shishir for your prompt and correct suggestion.



Have a great day



D Java


Thank you D Java,



Happy to know provided suggestion helped. Have a great day too.