0

I am running a SOQL query in a controller where I have declared 'with sharing' at the class level. The returned results are not following the sharing rules setup for the current user. The sharing rules are determined in this case by role. The following is my query and as you can see, I am setting the values of a picklist with the results.

    public List<selectOption> getPricebookSelectOptions(){
    List<SelectOption> options = new List<SelectOption>();
    List<Pricebook2> pBook = new List<Pricebook2>([Select Name, isActive FROM Pricebook2]);
    for(Pricebook2 p : pBook){
        options.add(new SelectOption(p.Id,p.Name));
    }
    return options;
}

Looking for ideas as to why all records are being returned. When I access Pricebook records as the logged in user via the Opportunity I only see the pricebook available to me so I am sure the permissions are created appropriately. Wondering if this is something to do with Pricebook records? Is there is a workaround to limit the data by user?

2

Looks like a limitation of the platform. From Enforcing Sharing Rules:

There is no guarantee that a class declared as with sharing doesn't call code that operates as without sharing. Class-level security is always still necessary. In addition, all SOQL or SOSL queries that use PriceBook2 ignore the with sharing keyword. All PriceBook records are returned, regardless of the applied sharing rules.

Thanks to Chris Duncombe for your suggestion on the following thread: How to get Pricebook2 Sharing Rules using UserRecordAccess

  • If you include your citations as text rather than images, they are much more searchable. – Adrian Larson Jun 1 '17 at 20:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.