[SalesForce] Standard Controller getRecord() causing Authorization Error in Force.com page

I have created a Force.com site but I can't access a page with it.

I am getting the error below:
enter image description here

VF Page

<apex:page standardcontroller="Case" extensions="CActions" showHeader="false" sidebar="false" applyBodyTag="false" docType="html-5.0">
    TEST
</apex:page>

Apex Controller

global class CActions {
    public Case caseObj {get; set;}

    public CActions(ApexPages.StandardController controller) {
        if(controller.getRecord() != null) {
            caseObj = [SELECT Id, Type
                       FROM Case
                       WHERE Id = :controller.getRecord().Id];
        }   
   }
}

Few checks that I have done :

  • The VF page is set as Active Site Home Page on the site.
  • Yes, the page is in the Site Visualforce Pages list.
  • Access has been granted on the Apex class via Public Access Settings.
  • Case object has Read access.
  • Read and Edit rights added on field Type.

If I remove

if(controller.getRecord() != null) {
    caseObj = [SELECT Id, Type
               FROM Case
               WHERE Id = :controller.getRecord().Id];
}

, the page works fine on the Force.com site.
enter image description here

Note:

Page works great using …/apex/VFP_Test?id=500N0000004c8ts

Does not work with the Force.com site URL : https:// scrmtest.cs6.force.com/VFP_Test?id=500N0000004c8ts

Tried debugging but no luck:

  • System.debug('## controller record : ' + controller );

gives me

|USER_DEBUG|[23]|DEBUG|## controller record :
ApexPages.StandardController[Case]

  • System.debug('## controller record : ' + (Case)controller.getRecord() ); does not even appear in the log.

Any issue with my code/settings?

Update : Still need help with this one.

Best Answer

You can solve your problem by using below class code:

   global class CActions {
        public Case caseObj {get; set;}
        public CActions(ApexPages.StandardController controller) {
            caseObj =(Case)controller.getRecord();
            if(caseObj != null && caseObj.Id!=null) {
                caseObj = [SELECT Id, Type
                           FROM Case
                           WHERE Id = :caseObj.Id];
            }   
       }

}

There are access issues faced with sObjects in force.com sites and controller.getRecord() returns a sObject. So we can resolve issue by casting it to a Case object.

Related Topic