Reading list Switch to dark mode

    How To Get Current Page URL from Apex code

    Updated 16 July 2021

    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{
    
             /**
             * Webkul Software.
             *
             * @category  Webkul
             * @author    Webkul
             * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
             * @license   https://store.webkul.com/license.html
             */
    
        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

    Searching for an experienced
    Salesforce Company ?
    Find out More
    <apex:page controller="pageurlclass">
    
       <!-- 
            /**
             * Webkul Software.
             *
             * @category  Webkul
             * @author    Webkul
             * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
             * @license   https://store.webkul.com/license.html
             */
             -->
          
      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

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home

    Table of Content