Skip to main content
Post Undeleted by arut
Improved answer
Source Link
arut
  • 5.4k
  • 2
  • 13
  • 30

Ensure that the WHERE clause used in the above query doesn't drastically change from your original query used to fetch the product list, in order to avoid any additional pricebook IDs. Lets assume that the above query resulted in ancall this array ofas priceBookIds and should as shown in the 6 unique pricebook IDs. Youscreenshot below:

enter image description here

Now, you can use theupdate your JS code based on the sample below to convertmake it into an object.dynamic (note that I've retained the outer for loops as-is so it's easier to understand this code, but they can also be converted into functional constructs):

for(var priceBookIdsj = ['01s46000004XMWhAAO', '01se0000000sup8AAA', '01se0000000spgaAAA', '01se0000000spgkAAA', '01se0000000strOAAQ', '01se0000000sugVAAQ'];
var priceBookObject =0;j {};< 
priceBookIdsp.forEach((element, indexlength;j++) => { 
    if(index ===// 0)
this object will be used to store all Object.assign(priceBookObject,the {'stpb':pricebookId-Unitprice element});
details for a given elseproduct
      var objPrice Object.assign(priceBookObject,= {['pb'+index] : element});
});

This above code would give you an object array as show in the screenshot below:

enter image description here

Now, you can update your JS code as shown in the sample below to make dynamic:

for(var j = 0;j < p.length;j++){
    for(var i = 0; i < p[j].length; i++){       
        if(p[j][i]priceBookIds.Pricebook2IdforEach(pbID ===> priceBookObject.stpb){
            stpb.pushif(p[j][i].UnitPricePricebook2Id == pbID );{
        }
        else// if(p[j][i].Pricebook2IdHere ==we priceBookObject.pb1){
are creating an object literal with pricebook ID as the key and pb1.push(p[j][i].UnitPrice);
the unit price as value in the second parameter using object literal notation
        }
        else// if(p[j][i].Pricebook2IdAppend ==this priceBookObject.pb2){new object literal to objPrice using assign method
            pb2    Object.pushassign(objPrice, {[pbID]: p[j][i].UnitPrice});
             
     
     }
       }
 else if(p[j][i].Pricebook2Id == priceBookObject.pb3){
    });
    }
    pb3.push(p[j][i].UnitPrice);
    // Using object.assign, append all the pricebook entries 
 for a product to an empty object and }
push it into the data }array
    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,
        [priceBookObject.stpb] : stpb[j],
        [priceBookObject.pb1] : pb1[j],
        [priceBookObject.pb2] : pb2[j]},
        [priceBookObject.pb3] : pb3[j]objPrice)
    });
}
this.allProductColumns=columnData;

Ensure that the WHERE clause used in the above query doesn't drastically change from your original query to fetch the product list, in order to avoid any additional pricebook IDs. Lets assume that the above query resulted in an array of the 6 unique pricebook IDs. You can use the code below to convert it into an object.

var priceBookIds = ['01s46000004XMWhAAO', '01se0000000sup8AAA', '01se0000000spgaAAA', '01se0000000spgkAAA', '01se0000000strOAAQ', '01se0000000sugVAAQ'];
var priceBookObject = {}; 
priceBookIds.forEach((element, index) => { 
    if(index === 0)
        Object.assign(priceBookObject, {'stpb': element});
    else
        Object.assign(priceBookObject, {['pb'+index] : element});
});

This above code would give you an object array as show in the screenshot below:

enter image description here

Now, you can update your JS code as shown in the sample below to make dynamic:

for(var j = 0;j < p.length;j++){
    for(var i = 0; i < p[j].length; i++){
        if(p[j][i].Pricebook2Id == priceBookObject.stpb){
            stpb.push(p[j][i].UnitPrice);
        }
        else if(p[j][i].Pricebook2Id == priceBookObject.pb1){
            pb1.push(p[j][i].UnitPrice);
            
        }
        else if(p[j][i].Pricebook2Id == priceBookObject.pb2){
            pb2.push(p[j][i].UnitPrice);
             
         }
        else if(p[j][i].Pricebook2Id == priceBookObject.pb3){
            pb3.push(p[j][i].UnitPrice);
            
         }
    }
    columnData.push({ 
        "ProductCode" : this.allProductColumns[j].fields.ProductCode.value,
        "Name" : this.allProductColumns[j].fields.Name.value, 
        "Family" : this.allProductColumns[j].fields.Family.value,
        [priceBookObject.stpb] : stpb[j],
        [priceBookObject.pb1] : pb1[j],
        [priceBookObject.pb2] : pb2[j],
        [priceBookObject.pb3] : pb3[j]
    });
}
this.allProductColumns=columnData;

Ensure that the WHERE clause used in the above query doesn't drastically change from your original query used to fetch the product list, in order to avoid any additional pricebook IDs. Lets call this array as priceBookIds and should as shown in the screenshot below:

enter image description here

Now, you can update your JS code based on the sample below to make it dynamic (note that I've retained the outer for loops as-is so it's easier to understand this code, but they can also be converted into functional constructs):

for(var j = 0;j < p.length;j++){
    // this object will be used to store all the pricebookId-Unitprice details for a given product
    var objPrice = {};
    
    for(var i = 0; i < p[j].length; i++){       
        priceBookIds.forEach(pbID => {
            if(p[j][i].Pricebook2Id == pbID ){
                // Here we are creating an object literal with pricebook ID as the key and the unit price as value in the second parameter using object literal notation
                // Append this new object literal to objPrice using assign method
                Object.assign(objPrice, {[pbID]: p[j][i].UnitPrice});                
            }
        });
    }
    
    // Using object.assign, append all the pricebook entries for a product to an empty object and push it into the data array
    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)
    );
}
Post Deleted by arut
Source Link
arut
  • 5.4k
  • 2
  • 13
  • 30

There as many ways to do this. One way is to use object (literals), computed properties and some functional javascript. Note that you will need to tweak your code based on this sample code.

Looking at your code, you already have an array of products with each element containing an array of corresponding Pricebook entries. Now, you need to get a list of unique pricebook entries via apex call (or you can create an array of unique pricebook IDs within the JS code). For example, the SOQL would be something similar to the following:

select Pricebook2Id from PricebookEntry where IsActive = true group by Pricebook2Id

Ensure that the WHERE clause used in the above query doesn't drastically change from your original query to fetch the product list, in order to avoid any additional pricebook IDs. Lets assume that the above query resulted in an array of the 6 unique pricebook IDs. You can use the code below to convert it into an object.

var priceBookIds = ['01s46000004XMWhAAO', '01se0000000sup8AAA', '01se0000000spgaAAA', '01se0000000spgkAAA', '01se0000000strOAAQ', '01se0000000sugVAAQ'];
var priceBookObject = {}; 
priceBookIds.forEach((element, index) => { 
    if(index === 0)
        Object.assign(priceBookObject, {'stpb': element});
    else
        Object.assign(priceBookObject, {['pb'+index] : element});
});

This above code would give you an object array as show in the screenshot below:

enter image description here

Now, you can update your JS code as shown in the sample below to make dynamic:

for(var j = 0;j < p.length;j++){
    for(var i = 0; i < p[j].length; i++){
        if(p[j][i].Pricebook2Id == priceBookObject.stpb){
            stpb.push(p[j][i].UnitPrice);
        }
        else if(p[j][i].Pricebook2Id == priceBookObject.pb1){
            pb1.push(p[j][i].UnitPrice);
            
        }
        else if(p[j][i].Pricebook2Id == priceBookObject.pb2){
            pb2.push(p[j][i].UnitPrice);
            
        }
        else if(p[j][i].Pricebook2Id == priceBookObject.pb3){
            pb3.push(p[j][i].UnitPrice);
            
        }
    }
    columnData.push({ 
        "ProductCode" : this.allProductColumns[j].fields.ProductCode.value,
        "Name" : this.allProductColumns[j].fields.Name.value, 
        "Family" : this.allProductColumns[j].fields.Family.value,
        [priceBookObject.stpb] : stpb[j],
        [priceBookObject.pb1] : pb1[j],
        [priceBookObject.pb2] : pb2[j],
        [priceBookObject.pb3] : pb3[j]
    });
}
this.allProductColumns=columnData;

You will also need to update the columns passed to the lightning datatable.