You need to sign in to do that
Don't have an account?

Get Current Page URL in Apex Class (Full & Exact URL)
I need the complete URL of my VF page in controller class (apex class). I have tried the the following solution but to of no use:
string x = ApexPages.currentPage();
srting y = ApexPages.currentPage().getURL();
Neither of the above methods are giving me the exact URL (e.g., https://c.cs2.visual.force.com/apex/MasterDetail?contractID=2009050010&bu=Media) of the current page that I need to send in an autogenerated email.
Regards,
Rao
Can you get it by checking the HTTP Headers?
For example:
String ref = ApexPages.currentPage().getHeaders().get('Referrer');
Just a thought?
Thanks for your idea. I tried but it did not work.
Rao
Try the following:
string BaseURL = ApexPages.currentPage().getHeaders().get('Host');pageReference cView = new ApexPages.StandardController(c).view();string fullURL = BaseURL + cView.getUrl();
scrappy dog looks like your solution would work but you'd also have to manually iterate through all the page params and add them to the url (and even then I'm not sure you'd be guaranteed that they would be in the same order as originally). If you already have the page open the other option would be to pass the url to your controller via an actionFunction (i.e. call "document.location.href" and pass the value as a param to your controller).
@scrappydog
Tested your solution and it doesn't work.
(1) ApexPages.currentPage().getUrl() appears to always return a url with the '/apex' prefix (not wanted on Sites)
(2) There is no documented method (AFAIK) which includes any custom extension added to a Salesforce Site (e.g. in the format http://mySite-developer-edition.na3.force.com/extension/)
getHeaders('Host') only gives you "mySite-developer-edition.na3.force.com"
True that getURL() returns /apex/, though I think that was OK for the original question since I was adding the base URL and the view URL together. In the case of Sites I agree that this can be an issue.
I ran same tests from a Sites page to see the results. The actual URL on my browser was http://force2b.test.cs3.force.com/testSite/Test?id=32423423:
To get the exact URL in a sites scenario I think you'd need to combine Site.getCurrentSiteUrl() with ApexPages.currentPage().getUrl(), stripping out the /apex/ part in getURL.
Not exactly sure what you mean about an extension. If it's like a folder name, then yes that can be done when defining the site. The 'extension' part would be in the field for "Default Web Address". For example, my Site could be referenced at force2b.force.com/somename/, but I can also leave that field blank so my site is referenced at the root. This is how you can create multiple sites in an Org. I believe this is also the value returned by Site.getPrefix().
Mike
I also did some more testing myself. The addition of "/apex" won't actually a problem on Sites so long as you have the correct extension/folder name.
That is. This works:
http://force2b.test.cs3.force.com/testSite/Test?id=32423423:
This also works:
http://force2b.test.cs3.force.com/testSite/apex/Test?id=32423423:
If course, you might want to get rid of the extra 'apex/' for cosmetic reasons if someone else is going to see the URL.
I thanks for pointing out the getPrefix method. It does return that extension and somehow I missed it because I was looking for Sites documentation in the Visualforce instead of Apex docs.
This combination works perfectly fine to generate a link for my purposes:
You could also presumably do something like this for a full url that would work on both Sites and not on Sites:
That last one would probably need some tweaking
Interesting, what's the difference between "Referrer" and "Host"?
Wes
Referrer I think only exists on a page redirect and refers to the domain the page was redrected from? I'm pretty sure it is not always available in the headers, which include:
Never seen this documented anywhere comprehensively though...
That makes sense. I've only needed 'Host' myself, but it's always good to have options.
Wes
i think you can combine this both to get the complete URL of the page
ApexPages.currentPage().getHeaders().get('Host') + ApexPages.currentPage().getUrl();
You can use URL.getSalesforceBaseUrl().toExternalForm() to get the base url. Append the record id to this url and your problem would be solved.
controller of Page 1:
public PageReference wish() {
pagereference s=new pagereference('/apex/Aboutus');
s.getHeaders().put('id','1234');
return s;
}
Construtor of Page 2:
Public aboutus(){
string ss=ApexPages.currentPage().getheaders().get('id');
system.debug('mylog'+ss);
}
in debug log i am getting null during this action..
Suppose u have the whole URL as: < https://cs14.salesforce.com/apex/myVFpage?id=906F00000008w9wIAA >
String baseURL = URL.getSalesforceBaseUrl().toExternalForm(); // it will return: < https://cs14.salesforce.com >
String PageURL = ApexPages.currentPage().getUrl(); // it will return: < /apex/myVFpage?id=906F00000008w9wIAA >
So to get your whole exact URL you can write:
String wholeURL = baseURL+PageURL; // it will return: < https://cs14.salesforce.com/apex/myVFpage?id=906F00000008w9wIAA >