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
RstrunkRstrunk 

Without sharing keyword not behaving as I thought

I am a bit of a noob so my question may be simple for someone to answer.  I do give kudos and follow up on threads for those who care about that. 

 

Basically my issue is this:

 

I have a VF page, custom tab and APEX class that is meant to allow a user to go to the tab and see a related list of cases for their contact record.  Their contact record is under an account tha tthey are not supposed to have access to so I thought having the without sharing keyword in the class would work but I am getting an error when I go to that tab as that logged in user.  Since the class operates at the system level why am I getting error messages based on the sharing settings?  Any thoughts would be appreciated.  (Oh, the code works because when I go to that tab as the admin it works as intended)

 

Below is my VF page and Class.

 

<apex:page standardController="Case" extensions="CC_getContact">
    <apex:tabpanel >
        <apex:tab label="Cases">           
                <apex:relatedList list="Cases" subject="{!theContact}"/>
                <style type="text/css">.actionColumn {display:none; visibility:hidden}</style>          
        </apex:tab>
    </apex:tabpanel>

</apex:page>

 

//This Class is used to get the contact record of the current logged in user for use in a visualforce page.  


public without sharing class CC_getContact {
  public Contact theContact { get; set; } // You can use theContact on your page.

  public CC_getContact(ApexPages.StandardController controller) {
    Contact[] c = [SELECT Id FROM Contact WHERE FirstName = :UserInfo.getFirstName() AND LastName = :UserInfo.getLastName() and Email = :UserInfo.getUserEmail()];
    // Make sure you select all the fields you need.
    if(!c.isEmpty()) {
      theContact = c[0];
    }
  }
}

 

The target profile has access to the tab and VF page.  Is there some permission issues posibly?  

Best Answer chosen by Admin (Salesforce Developers) 
RstrunkRstrunk

Submitted a case to Salesforce Premiere support and got the problem resolved.

 

     There is a system permission on the user profile I needed to have selected.  The permission is "Author Apex"

All Answers

Avidev9Avidev9
When you "without sharing " it actually respects the sharing settings and doesnt run on system mode.

May be thats y you are getting error ?
Kiran  KurellaKiran Kurella
Try giving access to the Apex class for the target profile as well. Also add a system debug statement to make sure your contact list has values.

system.debug('\n ### contacts ' + c.size() + ' => ' + c);

If the problem still exists then I recommend you to post the error message?


Teach_me_howTeach_me_how
1. check the object permission on case in the said profile

2. check or add the profile to security of vf to have the profile access the page

3. same thing on number 2 but this time check it for the security of the class used in vf
RstrunkRstrunk

Submitted a case to Salesforce Premiere support and got the problem resolved.

 

     There is a system permission on the user profile I needed to have selected.  The permission is "Author Apex"

This was selected as the best answer