I'm calling an Apex method imperatively inside a wire function. The wire function is used to retrieve listview and it's data. I'm using data from this listview to retrieve pricebookentry fields. getPricebookEntry is calling an apex method to retrieve pricebookentry. I'm passing all this data into a lightning datatable. But what is happening is the adding columndata is occuring before the apex method call the column data is becoming undefined. How do I make sure apex call occurs first and then the columndata code. I'm new to this. Sorry if I haven't been clear. I'm pasting the code below for better understanding.
@wire(getListUi, {objectApiName: PRODUCT_OBJECT,listViewApiName: '$Value'})
lastView({error,data}){
if (data) {
var columnData = [];
var pb1,pb2,pb3,stpb,sup,tes;
var santosh=0;
this.allProductColumns=data.records.records;
for(var i=0;i<this.allProductColumns.length;i++){
getPriceBookEntry({productId: this.allProductColumns[i].id})
.then( r =>{
console.log('r',r);
r.forEach(p =>{
if(p.Pricebook2Id=="01s46000004XMWhAAO"){
this.tes=p.UnitPrice;
console.log(this.tes);
}else if(p.Pricebook2Id=="01se0000000sup8AAA"){
this.stpb=p.UnitPrice;
}
})
})
.catch(error =>{
this.message = 'Error received: code ' + error.errorCode + ', ' + 'message ' + error.body.message;
console.log(this.message);
});
columnData.push({ "ProductCode" : this.allProductColumns[i].fields.ProductCode.value,
"Name" : this.allProductColumns[i].fields.Name.value,
"Family" : this.allProductColumns[i].fields.Family.value,
"01s46000004XMWhAAO" : this.tes,
"01se0000000sup8AAA" : this.stpb
});
}
this.allProductColumns=columnData;
} else if (error) {
this.error = error;
}
}
Here tes,stpb,pb1,pb2... variables are pricebookentry prices which get updated after apex call. But the columndata.push is occuring before the apex call.
What to do? I don't know whom to ask. Someone please answer:(
columnData.push
just below the for each loop? Also noticed, there are other things like hardcoded id's in code, you might want to correct as a best practicecolumnData.push
below the for each loop. It's working but the columns for only that iteration are being displayed. How do I append each iteration column data?