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
Josh VJosh V 

User Pricebook Permissions

Hey All,

 

I'm creating a custom visualforce page(s) which allows users to select a subset of pricebookentries and insert them into an existing quote.  Before I show the available products, I need them to select a pricebook.  I only want to show each user the pricebooks that they are allowed access to in a select list. Is there a way for me to get a List of all of the pricebooks that a user is allowed access to?  Is there an object that I can query which holds this data?  I read somewhere on the boards to query PriceBookShare, but that is an invalid ObjectId

 

Thanks for any help in advance!

 

Josh

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Use a query, as follows:

 

public List<SelectOption> getSelectBranchOptions() {
  List<SelectOption> options = new List<SelectOption>();
  for(pricebook2 pb:[select id,name from pricebook2 where isactive = true]) {
    options.add(new SelectOption(pb.id,pb.name));
  }
  return options;
}

You could make other queries based on other conditions, but this should be a good starting point.

All Answers

sfdcfoxsfdcfox

Not all objects are supported in Apex Code natively (yet). Try using the "with sharing" keyword on your controller, which should honor the pricebook sharing model.

Josh VJosh V

I'm using with sharing at the moment.  The problem is that I'm probably not using the correct method for selecting a pricebook...  I created a custom drop down with a list of all of the pricebooks, so they all show up for everyone.  

 

This is what I have for the drop down list right now.  As you can see, there is no way for the system to limit selections based on what the user has access to.

 

public List<SelectOption> getSelectBranchOptions() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('01s80000000AwcSAAS','Aurora Price Book'));
        options.add(new SelectOption('01s80000000As3VAAS','Oregon Coast Price Book'));
        options.add(new SelectOption('01s80000000Ar72AAC','Bend Price Book'));
        options.add(new SelectOption('01s80000000As3kAAC','Caldwell Price Book'));
        options.add(new SelectOption('01s80000000As3aAAC','Creswell Price Book'));
        options.add(new SelectOption('01s80000000As3bAAC','Medford Price Book'));
        options.add(new SelectOption('01s80000000As3QAAS','Olympia Price Book'));
        options.add(new SelectOption('01s80000000Ar77AAC','Woodland Price Book'));
        options.add(new SelectOption('01s8000000077T7AAI','Mid-Columbia Valley Price Book'));
        options.add(new SelectOption('01s80000000MWusAAG','California Price Book'));
        return options;
    }

 Is there already a method setup to select pricebooks that would conform to the "with sharing" call?

 

Thanks

sfdcfoxsfdcfox

Use a query, as follows:

 

public List<SelectOption> getSelectBranchOptions() {
  List<SelectOption> options = new List<SelectOption>();
  for(pricebook2 pb:[select id,name from pricebook2 where isactive = true]) {
    options.add(new SelectOption(pb.id,pb.name));
  }
  return options;
}

You could make other queries based on other conditions, but this should be a good starting point.

This was selected as the best answer
Josh VJosh V

Genius.  This works perfectly since the page will only return Ids which the user has access to.  Thank you so much!