Above error is coming in console.
Below is the code:
@track calculateTotal;
@track error;
@track results;
@track _shippingcharges;
@track _salestax;
@track _productarray;
@api set shippingcharges(value) {
this._shippingcharges = value;
this.doCalc();
}
get shippingcharges() {
return this._shippingcharges;
}
@api set salestax(value) {
this._salestax = value;
this.doCalc();
}
get salestax() {
return this._salestax;
}
@api set productarray(value) {
this._productarray = value;
this.doCalc();
}
get productarray() {
return this._productarray;
}
doCalc() {
if ((this.shippingcharges !==undefined) && (this.salestax !== undefined) && (JSON.parse(JSON.stringify(this.productarray)) !== undefined)) {
console.log(this.shippingcharges);
console.log(this.salestax);
console.log(JSON.parse(JSON.stringify(this.productarray))); // getting error on using JSON
console.log('I am in doCalc');
calculateTotal({ shippingcharges: this.shippingcharges, salestax: this.salestax, pricebookProductArray: JSON.parse(JSON.stringify(this.productarray)) })
.then(result => {
this.calculateTotal = result;
})
.catch(error => {
this.error = error;
});
}
}
Also, records in productArray are going to apex, but with all fields null. (the main problem)
Below is apex method:
@AuraEnabled(cacheable=true)
public static decimal calculateTotal(Decimal shippingcharges, Decimal salestax, List<PriceBookEntry2> pricebookProductArray){
System.debug(shippingcharges);
System.debug(salestax);
System.debug(pricebookProductArray);
Decimal total = 0;
Decimal calculatedPrice = 0;
for(PriceBookEntry2 pbe : pricebookProductArray){
System.debug('pbe.subtotal:'+pbe.subtotal);
total = total + pbe.subtotal;
}
system.debug('total:'+total);
calculatedPrice = total + salestax + shippingcharges;
return calculatedPrice;
}
public class PriceBookEntry2{
@AuraEnabled
public String priceBookEntryId;
@AuraEnabled
public String productId;
@AuraEnabled
public String productName;
@AuraEnabled
public String productCode;
@AuraEnabled
public String priceBookId;
@AuraEnabled
public String priceBookName;
@AuraEnabled
public Decimal priceUnit;
@AuraEnabled
public Integer quantity;
@AuraEnabled
public Decimal subtotal;
public PriceBookEntry2(){
priceUnit = 0;
quantity = 0;
subtotal = 0;
}
}
}