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

Hi Brian, 

 

I'm not sure why is it not working on my end, It is not updating the field on my user profile.

Should I keep changing my network to make it work?

 

or 

 

is there a way that we can use the syslog_transaction table like the first one and add a condition that only run when user logs in?

 

Thanks,

 

J

When I created my u_ip_address filed I created as type IP address.  If you did not and it is a string you may just need go put gr.remote_ip.toString();

I added the .toString(); but still not working. I keep on changing my network back and forth but still stay the same. 

 

is there any other way that we can use?

 

Thank you so much for your response, each of them really helps me a lot! 

Have you tried my new code below in a response below?

Strange this is not working for me anymore either.  I'm wondering if ServiceNow has something to prevent you from searching the logs.  I noticed when I was originally trying this on the log table it would not run the business rule at all even if I had not conditions.