[SalesForce] Create a trigger to get current IP address

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

Thanks

Best Answer

I have used the below code to get the IP address of the logged/current user. Hope this helps. Thanks

    Map<String, String> headersMap = ApexPages.currentPage().getHeaders();  

    // True-Client-IP has the value when the request is coming via the caching integration.
    String ipAddress = headersMap.get('True-Client-IP');

    if (String.isEmpty(ipAddress)) {
        // X-Salesforce-SIP has the value when no caching integration or via secure URL.
        // get IP address when no caching (sandbox, dev, secure urls)
        ipAddress = headersMap.get('X-Salesforce-SIP');
    } 

    if (String.isEmpty(ipAddress)) {
        // get IP address from standard header if proxy in use
        ipAddress = headersMap.get('X-Forwarded-For');
    } 

    return ipAddress;
Related Topic