function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tobibeertobibeer 

How to verify whether a VisualForce page exists?

How can I programatically verify whether or not a page by the name of "xyz" exists?

 

Neither the ApexPages namespace nor the PageReference object seem to provide any such method.

Best Answer chosen by Admin (Salesforce Developers) 
WizradWizrad

Reading is not my strong suit.

 

[select Id, Name from ApexPage where name = 'MyPage']

 

Works for me.

 

Keep im mind im running this query via SOQL Explorer.  I wouldnt be suprised if you couldnt instantiate an ApexPage or class within your code.  Try a count() query.

All Answers

WizradWizrad

[select Id from ApexClass WHERE Name = 'MyClass']

tobibeertobibeer

Doesn't seem to work. With...

  • ApexClass somepage = [select Id from ApexClass WHERE Name = 'SomeValidPageName'];


...I always get...

  • System.QueryException: List has no rows for assignment to SObject


...whether a page of that name exists or not.

WizradWizrad

Reading is not my strong suit.

 

[select Id, Name from ApexPage where name = 'MyPage']

 

Works for me.

 

Keep im mind im running this query via SOQL Explorer.  I wouldnt be suprised if you couldnt instantiate an ApexPage or class within your code.  Try a count() query.

This was selected as the best answer
tobibeertobibeer

Thanks, that did it. Now I can simply wrap the lot in a try catch statement to handle non-existing pages. Great!