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
GordymanLG2GordymanLG2 

Getting Current User IP Address from within a Trigger

Is it possible to get the current user's IP address from within a trigger?

 

Here is a snippet from a trigger I wrote to test the use of the ApexPages method (discussed in another posting thread). 

 

 

trigger SetUserIPAddress on Case (before insert) {

    PageReference pageRef = ApexPages.currentPage();
    Map<String,String> MapHeader = new Map<String,String>(pageRef.getheaders());

 

When I create a new Case invoking this trigger, I get a null page reference error as follows:

 

Apex trigger SetUserIPAddress caused an unexpected exception, contact your administrator: SetUserIPAddress: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.SetUserIPAddress: line 4, column 59

 

 

Can I only access this method from within a class controller and not from within a trigger?  nce I figure out how to get the URL header data into the map, I am good to go on extracing the correct IP address from the values in the Map.  Any help would be appreciated.

 

GordymanLG

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Hey

 

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.

 

Wes

All Answers

WesNolte__cWesNolte__c

Hey

 

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.

 

Wes

This was selected as the best answer
GordymanLG2GordymanLG2

That seems to be true.

 

I've figured out how to do this within a controller, which is invoked by overriding the Case View with a VF page.  But this removes in-line editing functionality from the standard Case View page.  So that is not an option.

 

Does anyone know of a mechanism for invoking a controller without doing so with a VF page?  I hope this isn't a stupid question.

 

Mike Gordon

aka GordymanLG2

WesNolte__cWesNolte__c

You can certainly instantiated the controller, as you would with any other class. If it's an extension (and I think yours is) you could try the ffg:

 

 

Apexpages.standardcontroller controller = new Apexpages.standardcontroller(<your object>);
MyController pageController = new MyController(controller);

 

 

I'm not sure if you can instantiated the standard controller from the trigger. You other option would be to more the functionality into another class that isn't a controller, and use this utility class in the controller and the trigger.

 

Wes