0

I need to compare a list of pricebook IDs in a pricebookentries of a product with a list of PricebookIDs. Is it possible in HTML template? I'm attaching a image to get an idea of the table.

enter image description here

Here PB1, Better... PB5, Super are pricebooks. I have a list with Products and another list with Pricebook Ids. If the pricebook Id of product and pricebook id from second list are equal, the unitprice value should be posted in the table.

How do I achieve this?

<table class = "slds-table slds-table_cell-buffer slds-table_bordered">
        <thead>
                <tr class = "slds-line-height_reset">
                    <th class = "" scope ="col">
                        <div class = "slds-truncate" title = "Product Name">Product Name</div>
                    </th>
                    <th class = "" scope ="col">
                        <div class = "slds-truncate" title = "Product Code">Product Code</div>
                    </th>
                    <th class = "" scope ="col">
                        <div class = "slds-truncate" title = "Family">Family</div>
                    </th>
                    <template for:each={newpbValues} for:item="pb">
                        <th key={pb.value}>
                            <div class = "slds-truncate" title = "{pb.Name}">{pb.label}</div>
                        </th>
                    </template>
                    </tr>
        </thead>
        <tbody>
            <template for:each={allProductColumns} for:item="prod" for:index="index">
                <tr key={prod.Id} >
                    <td>
                        <div class="slds-truncate" title="XYZ" >{prod.Name}</div>
                    </td>
                    <td>
                        <div class="slds-truncate" title="MNR" >{prod.ProductCode}</div>
                    </td>
                    <td>
                        <div class="slds-truncate" title="XXX" >{prod.Family}</div>
                    </td>
                    <template for:each={pbIds} for:item="pb">
                        <td key={pb.value}>
                            <div class="slds-truncate" title={pb.value} >{prod.pb}</div>
                        </td>
                    </template>
                </tr>
            </template>

Javascript code snippet:

@wire(getListUi, {objectApiName: PRODUCT_OBJECT,listViewApiName: '$Value'})
async lastView({error,data}){
    if (data) {
        var columnData = [];
        var temp = [];
        var pb1 = [];
        var pb2 = [];
        var pb3 = [];
        var stpb = [];
        var sup = [];
        var tes = [];
        this.allProductColumns=data.records.records;
        const pbEntryPromises = [] ;
        for(var i=0;i<this.allProductColumns.length;i++){
            pbEntryPromises.push(getPriceBookEntry({productId: this.allProductColumns[i].id}));
        }
        const p = await Promise.all(pbEntryPromises);
        console.log(p);
        console.log(this.newpbValues);
        for(var j = 0;j < p.length;j++){
            var objPrice = {};
            for(var i = 0; i < p[j].length; i++){
                this.newpbValues.forEach(pbId => {
                    Object.assign(objPrice, {[pbId.label]:0,[pbId.idvalue]:pbId.label});
                }); 
            }
            for(var i = 0; i < p[j].length; i++){
                this.newpbValues.forEach(pbId => {
                    if(p[j][i].Pricebook2Id == pbId.idvalue){
                        Object.assign(objPrice, {[pbId.label]: p[j][i].UnitPrice});
                    }
                }); 
            }
            console.log(objPrice);

                columnData.push(Object.assign({}, { 
                    "ProductCode" : this.allProductColumns[j].fields.ProductCode.value,
                    "Name" : this.allProductColumns[j].fields.Name.value, 
                    "Family" : this.allProductColumns[j].fields.Family.value}, objPrice)
                );

        }
        this.allProductColumns=columnData;
        console.log(columnData);
    } else if (error) {
        this.error = error;
    }
}

This is the data in allProductColumns. Each row in it contains the values of a row in the table shown above. How do I let the code understand under which Column to display the UnitPrice.

enter image description here

P data:

enter image description here

1

You can create a getter in js file lets say "get pricebookExists" which can return a Boolean value after checking if the list of pricebook includes the produc.pricebook.id or not.

Once you create above mentioned method, you can use if conditional check on the getter and do whatever you want inside it.

Please post whatever code you try and let me know if you struggle or have any confusion with the code. Not posting the code as you should try it first.

5
  • I'm pretty new to Salesforce. I don't know about getters. I edited the post to add my code. Could you please suggest changes in it or give an example code. – Nikhil Rayala Aug 24 '20 at 15:15
  • Can you add more details mentioned below: 1. What is pbIds and what data it holds. It is used in inner loop of tr td of template. 2. What does newpbValues hold and its format. 3. What are you getting in p from 'const p = await Promise.all(pbEntryPromises);' call – Arpit Aug 24 '20 at 18:06
  • 1. pbIds are the list of all pricebook IDs. I'm using it to display the columns in the table and also trying to make comparisions using it. 2. newpbValues format: {"idvalue":this.pbValues[i].Id,"label":this.pbValues[i].Name}. List of all pricebooks and it's name in the above format 3. I'll add p format in the post. – Nikhil Rayala Aug 24 '20 at 18:18
  • P is a 2 dimesional array. Each row consists of a list of pricebook entries of a products. – Nikhil Rayala Aug 24 '20 at 18:28
  • Do I need to clarify anything else in the code? – Nikhil Rayala Aug 25 '20 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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