function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dhananjaya BulugahamulleDhananjaya Bulugahamulle 

Create a trigger to get current IP address

I want to create a trigger when user make changes to record, then I want record their current IP. Does anybody know how that works?

Thanks
Best Answer chosen by Dhananjaya Bulugahamulle
Amit Chaudhary 8Amit Chaudhary 8
Hi Dhananjaya Bulugahamulle ,

You can try below code in VF page to get IP address.
String ipAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
'True-Client-IP' - when the request is coming via the caching integration.
'X-Salesforce-SIP' - when the request is not via caching integration (sandbox, developer edition orgs) or via the secure url.

Unfortunately triggers are not aware of Visualforce pages, they seem to operate a lot closer to the database (an educated guess really). What this means is that the system methods available for visualforce pages aren't going to do very much at a trigger level
https://developer.salesforce.com/forums/?id=906F00000008xy4IAA

I think you should check login history to get API Address in Trigger by below query
List<LoginHistory > lst= 
[SELECT UserId, LoginTime , SourceIp ,NetworkId from LoginHistory where UserId =:userinfo.getuserid() order by LoginTime desc limit 1];
System.debug('---->'+lst);
Please let us know if this will help you.

Thanks,
Amit Chaudhary

All Answers

surasura
try below but it works with contollers, extension related to pages  not in triggers, but  if your operation is initated in a visualforce page you can use static variable or a object field to pass the value to tigger
 
String ipAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');

 
Dhananjaya BulugahamulleDhananjaya Bulugahamulle
Hey, the problem is we want to get the source IP field from login history object. We do not know how to get it. We can get the IP but the system randomly chooses a one. Do you have any idea about it?
Amit Chaudhary 8Amit Chaudhary 8
Hi Dhananjaya Bulugahamulle ,

You can try below code in VF page to get IP address.
String ipAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
'True-Client-IP' - when the request is coming via the caching integration.
'X-Salesforce-SIP' - when the request is not via caching integration (sandbox, developer edition orgs) or via the secure url.

Unfortunately triggers are not aware of Visualforce pages, they seem to operate a lot closer to the database (an educated guess really). What this means is that the system methods available for visualforce pages aren't going to do very much at a trigger level
https://developer.salesforce.com/forums/?id=906F00000008xy4IAA

I think you should check login history to get API Address in Trigger by below query
List<LoginHistory > lst= 
[SELECT UserId, LoginTime , SourceIp ,NetworkId from LoginHistory where UserId =:userinfo.getuserid() order by LoginTime desc limit 1];
System.debug('---->'+lst);
Please let us know if this will help you.

Thanks,
Amit Chaudhary
This was selected as the best answer