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
<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
Be the first to comment.