Our Blog

How To Get Current Page URL from Apex code

How To Get Current Page URL from Apex code

In this post, I am going to share the information How To Get Current Page URL from Apex code in Salesforce. So 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 pageurlclass{
    public  String  headerdata{get;set;}
    public string urlvalue{get;set;}
    public string url{get;set;}
        
    public pageurlclass(){
        
         headerdata= ApexPages.currentPage().getHeaders().get('Host');
        
         urlvalue=Apexpages.currentPage().getUrl();
        
         url='https://' + headerdata+ urlvalue;
    }
  

}

 

Visualforce Page : Create visualforce page with name Pageurl

<apex:page controller="pageurlclass">
      
  1.  {!headerdata}

  2.  {!urlvalue}

  3.  {!url}

</apex:page>

Output

1. c.ap2.visual.force.com

2. /apex/Pageurl

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

Read More: Test Classes In Apex Salesforce

Leave a Comment

Comments (0)

Please verify that you are not a robot.

Welcome back

Welcome back! Please enter your details

One or more fields have an error. Please check and try again.

Forgot Password?

Tell us about Your Company

How can we help you with your business?