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:
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:
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;