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

If this is an after insert business rule I think you need to change your code.

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

	// Add your code here
	var gr = new GlideRecord('sys_user');
	gr.addQuery('user_name', current.sys_created_by);
	gr.query();
	if (gr.next()){
		gr.u_ip_address = current.remote_ip;
		gr.update();
	}

})(current, previous);

Another thought just came to mind.  Every time someone does something a new log is created.  So lets say they login (new Log) > Then they go to a list of their incident (new log) > Then they open an incident (new Log).  The Business rule just ran 3 times in a short period of time.  You put that with the number of users interacting in the system and you may end of with a performance issues.

I can not agree more. This  will take heavy toll on Infrastructure. You need to think differently to resolve your business case. Can you tell us what exactly are you trying to achieve here ? 

Hi Rajneesh,

 

It's kind of hard to explain but I will try my best to explain it to you guys.


What I'm trying achieve here is to get the IP Address from the Transaction Log Entry and add it the User Profile then I will use REST API Explorer to get that IP Address value and provide an API Url to our external dev team.

 

What they will do is block the IP address from accessing the SvN if the user failed to enter something from the system (this is on their end, so I don't really need to explain this further)

 

Back to my part, What I really want to achieve here is, after getting the IP Address from the Transaction Log Entry I will sent it to an external iFrame. Is that even possible?


That's why I chose to put it to user profile and update it every time user logs in because I can pull that data using REST API and provide an API url instead of sending it to an iFrame, which I'm not really sure if that is possible.

 

I hope you get my point, If you have further questions let me know. So we can discuss and have this question resolved.

 

Thank you for your response! 

Rather than updating IP address in User Profile, You can create API directly for Transaction Log table

Brian Lancaster
Tera Sage

I got it to run using the sys_user table.  I was thinking you really only need the IP when they login. So here is what I did.

Created and After Update business rule
find_real_file.png

On the advanced tab for condition I put the following.
current.last_login_time.changes()

Then this is the script

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

	// Add your code here
	gs.log('in Business rule');
	var gr = new GlideRecord('syslog_transaction');
	gr.addEncodedQuery('sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^sys_created_by=' + current.user_name);
	gr.query();
	if (gr.next()){
		gs.log(gr.remote_ip);
		current.u_ip_address = gr.remote_ip;
		current.update();
	}

})(current, previous);

You should test it multiple times logging in from different locations so to force a different IP Address (company network and home network).