How to get the IP Address from Transaction Log Entry and add it to a field in User Table

Jehiellb
Giga Contributor

Hi Team,

 

I have a requirement to get the IP address (remote_ip) from the Transaction Log Entry and display it to a field in User profile.

find_real_file.png

I want to get this value from Transaction Log Entry - IP Address (remote_ip)

and display it on IP address (u_ip_address) field in my User profile

find_real_file.png

 

I tried creating a After - Update Business rule and assigned it to User Table then I used this script 

current.u_ip_address = gs.getSession().getClientIP().toString();
current.update();

I also tried to create onChange Client Script using this script

var ipAddress = g_form.getSession().getClientIP().toString();
g_form.setDisplayValue('u_ip_address' , ipAddress);


Both of them did not work. I need some help to know what else to add on my script to make it work.
What should I add or remove on my script? If you could help with the scripting that would be a big help.

 

Your answer on this will be much appreciated, Thank you in advance.

1 ACCEPTED SOLUTION

I think I got it and it is more consistent.  I add more to the encoded query and a sort so that we got the most recent log activity otherwise I was not getting the correct IP all the time.

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

	// Add your code here
	var gr = new GlideRecord('syslog_transaction');
	gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^sys_created_by=' + current.user_name +
					   '^urlSTARTSWITH/home.do'); //added urlSTARTSWITH/home.do so limit the number of items brought back
	gr.orderByDesc('sys_created_on'); //Added sort so that the newest ones are at the top
	gr.setLimit(1); //Set limit to 1 which should bring back the most recent log that matches the encoded query
	gr.query();
	if (gr.next()){
		current.u_ip_address = gr.remote_ip;
		current.update();
	}

})(current, previous);

View solution in original post

16 REPLIES 16

Brian Lancaster
Tera Sage

What table do you have your BR running on?

Hi Brian,

 

I used User Table (sys_user) for my BR. Is this right?
find_real_file.png

 

Your help is much appreciated. Thank you!

Running it there would not get anything.  What conditions would it run under?  Unless something was updated on the sys_user table the BR would never be triggered.

I changed it to Transaction Log Entry (syslog_transaction) and it works! However when other user logged in, the IP address on their profile did not update. Do they need to have transaction before that field to update?

Or

Is there a way that to get the IP address of current logged in user right after they logged in?