Wednesday, March 29, 2017

How To Get Current Page URL from Apex code in Salesforce

To implement this we need to use Apexpages.currentPage() methods.

Get Current Page URL
getHeaders() : It return a map of the request headers, where the key string contains the name of the header, and the value string contains the value of the header.
After that get the ‘Host’ key value from that map

getUrl() : It returns the relative URL associated with the PageReference when it was originally defined, including any query string parameters and anchors.
Apex Code :


public with sharing class currentpageurl{
    public  String  headerdata{get;set;}
    public string urlvalue{get;set;}
    public string url{get;set;}
         
public currentpageurl(){      
headerdata= ApexPages.currentPage().getHeaders().get('Host');
urlvalue=Apexpages.currentPage().getUrl();
url='https://' + headerdata+ urlvalue;
    }  
}
Visualforce Page : 

<apex:page controller="currentpageurl"> 

1. {!headerdata} 
2. {!urlvalue} 
3. {!url} 

</apex:page>

 Output:

1. c.cs3.visual.force.com

2. /apex/Pageurl

3. https://c.ap2.visual.force.com/apex/Pageurl

No comments:

Post a Comment