+ Start a Discussion
davixdavix 

Variable does not exist: ApexPages

I must be missing something but I'm new enough to not know what it is.  Thanks for any help.
Error : Variable does not exist: ApexPages
 
public class myClass {

    public String getIP() {
        String ip = null ;
        /* Don't work
         * String ip = ApexPages.currentPage().getHeaders().get('True-Client-IP');
        if (ip == '') {
            ip = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
        }*/
        system.debug(ApexPages);
        Map<String, String> entetes = ApexPages.currentPage().getHeaders();
        if(entetes != null) {
            ip = entetes.get('True-Client-IP');
            system.debug('-------Client IP------'+ip);
            if(ip == null)
                ip = entetes.get('X-Salesforce-SIP');
                system.debug('-------Salesforce IP------'+ip);  
        }
       return ip ;
    }
}


 
Best Answer chosen by davix
Kapil KaushikKapil Kaushik
As I have gone through with the code you have posted. It does not seems that you can use ApexPages.something().something().something() with this scenerio. As I told you the only way by which ApexPages.something().something().something() can work when you are actually opening that VF page in browser by querying its valid url.
Because when you are going to call ApexPages.currentPage()........ here the link breaks becuase there is not any active page so of which page's headers it should returns, hence you got the NullPointerException.

I hope you got the Problem and understanded the concept behind it.

So you have to find some other way to get the IP address of the current logged In user. May be you should use any workaround at the CTI side or something else.

Well, I'll try to find something for you. Stay tuned.

Thanks

 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi David,

ApexPages is not a variable to print it is a class so you cannot use
        system.debug(ApexPages);

instead use 
system.debug(
ApexPages.currentPage().getHeaders().get('True-Client-IP')); ​
Please refer below links for more information on ApexPage.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_apexpages.htm

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/apex_System_ApexPages_currentPage.htm


Hope this helps you!

Please accept my solution as Best Answer if my reply was helpful. It will make it available for other as the proper solution. If you felt I went above and beyond, you can give me kudos.
 
Thanks and Regards
Sandhya

 
Kapil KaushikKapil Kaushik
Hi david,

Error is saying something meaningful. Because ApexPages is a class and we can not debug or pring something when we are directly trying to print the class name.

So if you comment out line no. 10 then your code will compile successfully.

Note - We can debug or print the object of any class but not direct class name.

Hope this helps. Please revert back to me if you have any further queries & Please mark this answer as the right answer so that others can get benifit from this.

Thanks,
Kapil Kaushik
davixdavix
Thanks, I removed this line, but a got : 
"System.NullPointerException: Attempt to de-reference a null object"

It is for this reason that I had added this line.
davixdavix
It tried with only  : 
public String getIP() {
        String ip = null ;
        system.debug(ApexPages.currentPage().getHeaders().get('True-Client-IP'));
        return ApexPages.currentPage().getHeaders().get('True-Client-IP') ;
}
but I got the same problem
 
SandhyaSandhya (Salesforce Developers) 
Hi David,

On which line you are getting this error

Thanks
Sandhya
SandhyaSandhya (Salesforce Developers) 
Hi David,

You should just be able to put a value on the headers map:
 
PageReference.getHeaders().put('Date', '9/9/99');

please refer below link.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_getHeaders.htm

Thanks and Regards
Sandhya
davixdavix
public String getToken() {
        return ApexPages.currentPage().getHeaders().get('True-Client-IP') ;
}

I tried with only this code, it don't work
Kapil KaushikKapil Kaushik
Hi David,

Your line - ApexPages.currentPage().getHeaders().get('True-Client-IP') ;  is not working in your case so you can replace the above line with the below one :

ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP') ;
Kapil KaushikKapil Kaushik
I think you are trying to find the IP of the current logged in user by using 'True-Client-IP' as the parameter, But we can also get the same by using the 'X-Salesforce-SIP'.

Is it worthy for you ? Please write

Thanks,
Kapil Kaushik
davixdavix
public String getToken() {
        return ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP') ;
}
I tried your solution, But it don't work for me.
davixdavix
same error :"System.NullPointerException: Attempt to de-reference a null object"
Ankit SehgalAnkit Sehgal
Call this statement in the constructor and it should work fine.
davixdavix
I tried in constructor, but I got the same error
public class ip {
	private String ip_client = null ;
    public ip(){
    	ip_client = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
        this.ip_client = ip_client ;
    }
}
and used in debug execute window :
ip c = new ip() ;
Error in first line : "ip_client = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');"
Kapil KaushikKapil Kaushik
Hi David,

One query - How you are trying this code ? Is it controller of any page ?  OR
 
You are trying to run it from Anonymous window of Developer console ?
Kapil KaushikKapil Kaushik
My query was How you are trying to run this code ?
Kapil KaushikKapil Kaushik
I am assuming that you are trying this as a controller of a VF page. am I right ?
davixdavix
I wish to use it from a "CTI Adapter URL". But I got the same problem with my "Force.com Developer Console / Debug / Open Execute Anonymous Window"
Kapil KaushikKapil Kaushik
Okay, I haven't told you to try this code in "Anonymous window", It will obviously going to give the same problem.

The reason is because there is not any page via which we can headers. So be sure that you are using this code as a controller of any VF page. If not, then please try to use that code as the controller and try to print "ip_client" property on the VF page.

And let me know for any further queries.

Finally I would request, if possible can you please post your more code snippet and the way how you are using it. We will get more clear and can suggest you some good workarounds.

Thanks for your quick reply.
Kapil KaushikKapil Kaushik
I am sure that you are misinterpreting the concept and you are not implementing them correctly.

If possible, Please post some more code and be sure that the things which comes after "ApexPages.something.something.something" is only going to work if that code is invoked from a VF page or somehow from test classes.

Please clear our queries.
davixdavix
APEX : 
public class ip {
   private String ip_client = null ;

    public ip(){
    	ip_client = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
        this.ip_client = ip_client ;
    }

   public String getIP() {
        String ip = ApexPages.currentPage().getHeaders().get('True-Client-IP');
	    if (ip == '') {
	        ip = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
        }
        return ip ;
   }

}



CTI adaptater content :
<html>
<head>
<script src="https://xxxxxx-dev-ed.my.salesforce.com/support/api/37.0/interaction.js"></script>
<script type="text/javascript">

var callback = function (response) {
    if (response.result) {
        alert(response.result);
    } else {
        alert(response.error);
    }
};

function runApex() {
    // sforce.interaction.runApex('ip', 'getIP', 'ip=192.168.1.1', callback);
   sforce.interaction.runApex('ip', 'getIP', '', callback);
}

</script>
</head>
<body>
<button onclick="runApex();">runApex</button>
</body>
</htm>



 
Kapil KaushikKapil Kaushik
As I have gone through with the code you have posted. It does not seems that you can use ApexPages.something().something().something() with this scenerio. As I told you the only way by which ApexPages.something().something().something() can work when you are actually opening that VF page in browser by querying its valid url.
Because when you are going to call ApexPages.currentPage()........ here the link breaks becuase there is not any active page so of which page's headers it should returns, hence you got the NullPointerException.

I hope you got the Problem and understanded the concept behind it.

So you have to find some other way to get the IP address of the current logged In user. May be you should use any workaround at the CTI side or something else.

Well, I'll try to find something for you. Stay tuned.

Thanks

 
This was selected as the best answer
davixdavix
I think I will actually use a CTI solultion side , but it seemed safer to me that the IP is detected by SalesForce .

Thanks