1

I'm in the process of creating a variable dependent picklist, where you can select a pricebook from a dropdown and then a multiselect picklist will show the products in that pricebook. So far, I have started a controller to retrieve and return a list of the pricebooks, but I keep receiving an error which reads:

"Compile Error: Constructor not defined: [System.SelectOption], (Schema.SObjectField, Schema.SObjectField) at line 17 column 25"

I'm still new to this language's syntax, so I'm not entirely sure what that means.

Here is my code:

String[] prod = new String[]{};

public PageReference test() {
    return null;
}

//Create the dropdown menu.
public List<SelectOption> getItems() {
    List<SelectOption> options = new List<SelectOption>();

    //Add blank option to display when the page loads.
    options.add(new SelectOption(' ', ' '));

    //Create options.        
    for (Integer i : pricebook2.name) {
        options.add(new SelectOption(pricebook2.name, pricebook2.name));
    }
    return options;
}

public String[] getProd() {
    return prod;
}

public void setProd(String[] prod) {
    this.prod = prod;
}

There appears to be something wrong with the loop which creates options. Does anyone know what's going on here?

Thanks in advance!

3

I'm not sure what you're trying to do here:

//Create options.        
for (Integer i : pricebook2.name) {
    options.add(new SelectOption(pricebook2.name, pricebook2.name));
}

What exactly is 'pricebook2'?

If it's a single pricebook, then you don't need a loop - just one new SelectOption. If it's a list of pricebooks, then you need to iterate through the list:

List<Pricebook2> MyPricebook2List = new List<Pricebook2>([SELECT Id, Name FROM Pricebook2 ORDER BY Name ASC]);
for (Pricebook2 pb2 : MyPricebook2List) {
    options.add(new SelectOption(pb2.name, pb2.name));
}

Also if you're planning to work with the selected pricebook, I would put the PB2 Id as the SelectOption value as opposed to the name, like so:

options.add(new SelectOption(pb2.Id, pb2.name));
2
  • Thanks for your reply. I was trying to access the object to list the price books, but I guess the value needs to be passed through pb2?
    – Alex
    Jun 26 '14 at 15:17
  • You'll need to query for the Pricebook2 records in your org, I'll update my answer now with an example. Jun 26 '14 at 15:19
1

I did that before using Lightning Web Component.

First, you need to select the list of Pricebooks from the database, to do that use an apex method that returns the list of Pricebooks(Id, Name) using a simple SOQL query as shown below:

 @AuraEnabled(cacheable=true)
    public static List<Pricebook2> getAllPricebooks() {
        return [SELECT Id, Name FROM Pricebook2];
    }

In your HTML page, use the lightning-combobox component to display the selected Pricebooks :

<lightning-combobox  name="Pricebooks"
                     label="Pricebooks"
                     placeholder="-Select Pricebook-"
                     value={value}
                     options={pricebookOptions}
                     onchange={handelPricebookChange} >
 </lightning-combobox>
  • pricebookOptions : is a JS array that holds the list of Pricebooks, so you need to declare it in you JS file with the @track decorator :

    @track pricebookOptions=[];

  • handelPricebookChange : The method that will be called when the user select a pricebook.

Now, you need to call your apex method, get the data and store it in your pricebookOptions property, that should be done in your .js, to do that :

  • Import your apex method :

    import getPricebooks from '@salesforce/apex/PricebookController.getAllPricebooks';

  • Run the code that allows you to get the Pricebooks automatically (after the lightning-combobox component is rendered) using the connectedCallback() method :

    connectedCallback(){
    
     getPricebooks().then(result => {
      for(let i=0; i<result.length; i++) {
         this.pricebookOptions = [...this.pricebookOptions,{result[i].Id 
         ,label: result[i].Name}];}              
        }).catch(error =>{// Handle errors});
    }
    

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .