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
Dastagiri BashaDastagiri Basha 

how to create a picklist for pricebook records in aura component. i want to display PriceBook Records as a picklist in AuraComponent, Can any one help me, i am a Beginner.

Controller apex:
public class newPriceBookEntry {
@AuraEnabled
    public static List<Pricebook2> getAllPricebooks() {
     
        return [SELECT Id, Name FROM Pricebook2];
    }
}

Component::
<aura:attribute name="PriceBooks" type="List" default="[]"/>

    <aura:handler name="init" value="{! this }" action="{! c.init }"/>
 <div class="slds-m-around_xx-large">
    <lightning:combobox name="general" label="Many Options" placeholder="Select an PriceBook" 
                        options="{!v.PriceBooks }" onchange="{! c.handleChange }"/>
</div>

Controller:JS::

init : function(component, event, helper) {
        var items = component.get("c.getAllPricebooks");
       var Options = [{ value: "v.PriceBooks", label: items }];
        component.set("v.PriceBooks", Options);
    }, 

Best Answer chosen by Dastagiri Basha
CharuDuttCharuDutt

Hii Dastagiri 

try below Code
 

({
	init : function(cmp, event, helper) {
          var action = cmp.get("c.getAllPricebooks");
 var options = [];
     
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
               
                 var arr = response.getReturnValue() ;
                arr.forEach(function(element) {
                    options.push({ value: element.Name, label: element.Name });
                });
				
        			cmp.set("v.PriceBooks", options);
    			
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });
        $A.enqueueAction(action);
    }
       
})

Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt

Hii Dastagiri 

try below Code
 

({
	init : function(cmp, event, helper) {
          var action = cmp.get("c.getAllPricebooks");
 var options = [];
     
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
               
                 var arr = response.getReturnValue() ;
                arr.forEach(function(element) {
                    options.push({ value: element.Name, label: element.Name });
                });
				
        			cmp.set("v.PriceBooks", options);
    			
            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        console.log("Error message: " + 
                                 errors[0].message);
                    }
                } else {
                    console.log("Unknown error");
                }
            }
        });
        $A.enqueueAction(action);
    }
       
})

Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
Dastagiri BashaDastagiri Basha
Thankyou Chardutt for your Response