Monday, January 2, 2017

Error occurred while loading a Visualforce page



    Hi SFDC Developers,
                      I was working on Force.com site and suddenly I got an error "Error occurred while loading a Visualforce page" 
I tried to put debug logs, checked code, tried everything that what could be the issue? but it has taken much more time of mine to find out exact cause about what was going wrong!!

Then I realized what should I do so that I can get a user-friendly message which will give me exact idea about what is causing the issue and will save my time.

Rescue Operation
             Two options available on Force.com sites which gives the exact reason the particular error has pop-up are
  1. $Site.ErrorMessage : Returns an error message for the current page if it’s a designated error page for the site and an error exists; otherwise, returns an empty string.
  2. $Site.ErrorDescription : Returns an error message for the current page if it’s a designated error page for the site and an error exists; otherwise, returns an empty string.
     Ref: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_global_site.htm

1. Create a custom VisualforcePage and a Controller

     VisualforcePage : CustomErrorPage
     
    <apex:page action="{!yourPageLevelAction}" controller="YourController" >
<apex:outputText styleClass="title" value="{!failingPageResponse}"> </apex:outputText>
    </apex:page>
    
    You can apply CSS as per your convinience

    Controller : YourController

    public  class YourController {
        //Variable Declaration
        public String failingPageResponse { get; set; }

        public PageReference yourPageLevelAction()
       {
          failingPageResponse = Site.getErrorDescription();
          return null;
        }//yourPageLevelAction
    }//YourController

Now you have your Custom error page and Controller ready!

2. Replace CustomErrorPage with standard Force.com site error pages

       Go to the site that you have created in your org and click on the label of that site and follow the steps mentioned following.


   i. Page Assignment
      


     Click on the Page Assignment button and replace the existing pages with CustomErrorPage.    
     

3. Set Use custom Visualforce error pages to True from Site Administration and hit Save

4. OutPut after setting Custom Error Pages
            I just updated my Visualforce-Page to generate exception for demoing you guys,
It will look something like following.



    That's it....!!!

    You are all set now enjoy debugging......!!!!
     
    
    

     

     
  





4 comments: