5

I want get current page name of visulforce page. I have tried this and it works for the page having no query parameters in the URL but it fails in the pages having one or more parameters. Any workaround will be much helpful. Here is my snippet.

String pageName = ApexPages.currentPage().getUrl().split('apex/')[1];

Thanks in advance!

6

Another solution

Use $CurrentPage.Name here

in VF page use

<input type="hidden" name="currentvfpage" value="{!$CurrentPage.Name}"/>

And in controller get the hidden input value

String pageName = ApexPages.currentPage().getParameters().get('currentvfpage');
system.debug('-----current page name----'+pageName);
12

How about:

String pageName = ApexPages.currentPage().getUrl().substringBetween('apex/', '?');

or, if you do not have parameters:

String pageName = ApexPages.currentPage().getUrl().substringAfter('apex/');

Check out the other String methods, there are lots to help in this regard.

  • Good to know that substringBetween even exists, but it returns null if the question mark is missing which is not helpful in this case. – Keith C May 22 '16 at 15:59
  • yes Correct @Keith – Krishna Bidwai May 22 '16 at 17:49
  • Edited, there are lots of string methods to help as mentioned in the asnwer – Phil Hawthorn May 22 '16 at 20:08
  • This will not work on a public site, use your 'siteName/' instead of 'apex/' on public sites. – Oles Malkov Feb 13 '17 at 14:55
  • Good to know @OlesMalkov – Phil Hawthorn Feb 13 '17 at 15:14
1

Here is a article describing How to get the Visual Force Page Name Using Apex Class? Hope this helps.

Code snippet from the link.

public string getVFPageName() {
     // THIS IS THE CLOSEST YOU CAN GET SO FAR
     String pageName = ApexPages.CurrentPage().getUrl();
     // IT ALWAYS STARTS WITH /APEX/ SO REMOVE IT
     pageName = pageName.replaceFirst('/apex/', '');
     pageName = EncodingUtil.urlEncode(pageName, 'UTF-8');
     // %3F IS THE VALUE OF THE QUESTION MARK IN UTF-8 
     string[] pageNameExtra = pageName.split('%3F', 0);
     // SO YOU MAY SPLIT THE STRING AT THAT POINT
     // FOR THIS PURPOSE YOU ONLY NEED THE FIRST
     // IN THE RESULTING ARRAY
     pageName = pageNameExtra[0];
     // HERE IS YOUR PRODUCT  
     system.debug('pageName-->' + pageName);
     return pageName;
}
  • 1
    Why there is a negative vote? – Saroj Bera May 24 '16 at 6:04
1

Just use RegExp to parse the VFP page name.

public String getVFPname(){
  return ApexPages.currentPage().getUrl().replaceAll('\\?.*', '').replaceAll('/apex/', '');
}
0

Small improvement to Phil Hawthorn's answer above:

String pageName = ApexPages.currentPage().getUrl().substringAfter('apex/').split('\\?')[0];

This alone should be able to handle page-names with and without parameters/querystring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.