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

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);

Kieran Anson
Kilo Patron
Hi, Are you just wanting to get the IP address the user is logging in with? If so, you can modify the installation exit script that is used for BasicAuth and SSO (depending on which you use) to modify the current user session and thus user record with the ID. Would this meet your requirements? This would only work for interactive sessions