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
Kamran-RaoKamran-Rao 

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

aalbertaalbert

Can you get it by checking the HTTP Headers?

 

For example:

String ref = ApexPages.currentPage().getHeaders().get('Referrer');

 

Just a thought?

Kamran-RaoKamran-Rao

Thanks for your idea. I tried but it did not work.

 

Rao

Force2b_MikeForce2b_Mike

Try the following:

 

 

string BaseURL = ApexPages.currentPage().getHeaders().get('Host');pageReference cView = new ApexPages.StandardController(c).view();string fullURL = BaseURL + cView.getUrl();

 

Mike

 

d3developerd3developer

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).

d3developerd3developer

@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"

 

 

 

Force2b_MikeForce2b_Mike

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

 

d3developerd3developer

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:

 

 

		    String fullUrl;
		    if (Site.getPrefix() != null)
		         fullUrl = Site.getPrefix() + Page.largeLookup.getUrl();
		    else
		    	fullUrl = Page.largeLookup.getUrl();

 

 

You could also presumably do something like this for a full url that would work on both Sites and not on Sites:

 

 

if(Site.getSiteUrl() != null)
  fullUrl = Site.getSiteUrl() + Page.testPage.getUrl();
else
  fullUrl = 'https://' + ApexPages.currentPage().getHeaders('Host') + Page.testPage.getUrl();

 

That last one would probably need some tweaking

 

 

 

 

WesNolte__cWesNolte__c

Interesting, what's the difference between "Referrer" and "Host"?

 

Wes

d3developerd3developer

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:

 

 

Accept-Charset
Accept-Encoding
Accept-Language
CipherSuite
Connection
Host
Keep-Alive
User-Agent
X-Salesforce-Forwarded-To
X-Salesforce-SIP



 Never seen this documented anywhere comprehensively though...

 

WesNolte__cWesNolte__c

That makes sense. I've only needed 'Host' myself, but it's always good to have options.

 

Wes

sujan12sujan12

i think you can combine  this both to get the complete URL of the page 

 

ApexPages.currentPage().getHeaders().get('Host') + ApexPages.currentPage().getUrl();

 

samruddhi_gokhalesamruddhi_gokhale

You can use URL.getSalesforceBaseUrl().toExternalForm() to get the base url. Append the record id to this url and your problem would be solved.

kumaresan.mkumaresan.m
I want to pass some parameters in url to another page with hidden way. so i tried using getheaders() instead of getparameters(). but this was not works. Please help me understand, how to do? i am pasting my code below:

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.. 

Debarghya SenDebarghya Sen
You can try the below example:

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 >

 
IT Support EnessIT Support Eness