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 

Hii all, i want build a component to Display PricebookEntry records when ever we choose PriceBook Record from the PricebookPicklist. Can plz Help me with the below code, its not displaing the Pricebook entry records

Apex Code::
public class newPriceBookEntry {
@AuraEnabled
    public static List<Pricebook2> getAllPricebooks() {
      
        return [SELECT Id, Name FROM Pricebook2];
    }
    @AuraEnabled
    public static List<PricebookEntry> getAllPriceBookEntries(string recordId){
  List<PricebookEntry> PentryList= [select id, Name, Product2.Name, UnitPrice from PricebookEntry Where Pricebook2.Name =:recordId];
        return PentryList;
    }        
}

Component::  
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" controller="newPriceBookEntry" >
    <aura:attribute name="PriceBooks" type="List" default="[]"/>
    <aura:attribute name="PriceBooksEntries" type="PricebookEntry[]" />
    <!-- <aura:attribute name="recordId" type="Id" />  -->
    
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
    <div class="slds-m-around_xx-large">
        <lightning:combobox name="general" label="Price Books" placeholder="Select an PriceBook" 
                            options="{!v.PriceBooks}" onchange="{!c.handleChange}"/>  
    </div>   
    <table class="slds-table slds-table_bordered slds-table_cell-buffer" >
        <thead>
            <tr class="slds-text-title_caps">
                <th scope="col">
                    <div class="slds-truncate"  title="S.no">S.no</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Employee Name">PriceBookEntryName</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Basic Salary">ProductName</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Attendance">UnitPrice</div>
                </th>
            </tr>
        </thead>
        <tbody> 
            <aura:iteration items="{!v.PriceBooksEntries}" var="epl" indexVar="count">
                <tr>
                    <td>
                        <div class="slds-truncate">{!count + 1}</div>
                    </td>
                    <td>
                        <div class="slds-truncate"><lightning:input type="text" value="{!epl.Name}"  /></div>
                    </td>
                    <td>
                        <div class="slds-truncate"><lightning:input type="text" value="{!epl.Product2.Name}" /></div>
                    </td>
                    <td>
                        <div class="slds-truncate"><lightning:input type="number" value="{!epl.UnitPrice}" /></div>
                    </td>
                    <td>
                        <lightning:button onclick="{!c.HandleSave}" variant="brand" label="Save" />
                    </td>
                </tr>
            </aura:iteration>
        </tbody> 
    </table> 
    
</aura:component>

Controller:: 

({
    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);
    },
    handleChange: function (cmp, event) {
        // This will contain the string of the "value" attribute of the selected option
       var action = cmp.get("c.getAllPriceBookEntries"); 
        var selectedOptionValue = event.getParam("value");
        alert(selectedOptionValue);
        action.setParams({recordId: selectedOptionValue});                  
               action.setCallback(this,function(response){
            var state = response.getState();
            alert(state);
            if (state === "SUCCESS"){
                var methodResponse = response.getReturnValue();
                alert(methodResponse);
                component.set('v.PriceBooksEntries',methodResponse);
            }else if (state === "INCOMPLETE") {
                alert('Response is Incompleted');
            }else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        alert("Error message: " + 
                              errors[0].message);
                    }
                } else {
                    alert("Unknown error");
                }
            }
        });
         $A.enqueueAction(action);
    }
})
 
Best Answer chosen by Dastagiri Basha
CharuDuttCharuDutt
Hiii Dastagiri
Try Below Code Now it's Working
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" controller="newPriceBookEntry" >
    <aura:attribute name="PriceBooks" type="List" default="[]"/>
    <aura:attribute name="PriceBooksEntries" type="PricebookEntry[]" />
    <!-- <aura:attribute name="recordId" type="Id" />  -->
    
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
    <div class="slds-m-around_xx-large">
        <lightning:combobox name="general" label="Price Books" placeholder="Select an PriceBook" 
                            options="{!v.PriceBooks}" onchange="{!c.handleChange}"/>  
    </div>   
    <table class="slds-table slds-table_bordered slds-table_cell-buffer" >
        <thead>
            <tr class="slds-text-title_caps">
                <th scope="col">
                    <div class="slds-truncate"  title="S.no">S.no</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Employee Name">PriceBookEntryName</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Basic Salary">ProductName</div>
                </th>
                <th scope="col">
                    <div class="slds-truncate" title="Attendance">UnitPrice</div>
                </th>
            </tr>
        </thead>
        <tbody> 
            <aura:iteration items="{!v.PriceBooksEntries}" var="epl" indexVar="count">
                <tr>
                    <td>
                        <div class="slds-truncate">{!count + 1}</div>
                    </td>
                    <td>
                        <div class="slds-truncate"><lightning:input type="text" value="{!epl.Name}"  /></div>
                    </td>
                    <td>
                        <div class="slds-truncate"><lightning:input type="text" value="{!epl.Product2.Name}" /></div>
                    </td>
                    <td>
                        <div class="slds-truncate"><lightning:input type="number" value="{!epl.UnitPrice}" /></div>
                    </td>
                    <td>
                        <lightning:button onclick="{!c.HandleSave}" variant="brand" label="Save" />
                    </td>
                </tr>
            </aura:iteration>
        </tbody> 
    </table> 
    
</aura:component>



#########################################


({
	   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);
    },
    handleChange: function (cmp, event) {
        // This will contain the string of the "value" attribute of the selected option
       var action = cmp.get("c.getAllPriceBookEntries"); 
        var selectedOptionValue = event.getParam("value");
        alert(selectedOptionValue);
        action.setParams({recordId: event.getParam("value")});                  
               action.setCallback(this,function(response){
            var state = response.getState();
            alert(state);
            if (state === "SUCCESS"){
                var methodResponse = response.getReturnValue();
                alert(methodResponse);
                cmp.set('v.PriceBooksEntries',methodResponse);
            }else if (state === "INCOMPLETE") {
                alert('Response is Incompleted');
            }else if (state === "ERROR") {
                var errors = response.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        alert("Error message: " + 
                              errors[0].message);
                    }
                } else {
                    alert("Unknown error");
                }
            }
        });
         $A.enqueueAction(action);
    }
})
Please Mark It As Best Answer If It Helps
Thank You!