+ 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!