0

enter image description here

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;
         }
    }
    }
2
  • 1
    i can see two things in your code, your apex method name defined during import of apex method and a public property matches. It might create issue. Also, can you put a log in your js and see if the array has value before calling apex Jul 8, 2020 at 15:15
  • I have changed the name of public property on your suggestion. There is already a log inside doCalc() just before calling calculateTotal apex method. It is giving me correct record with values in fields, but also along with an error in console mentioned here in post at top most.
    – user75908
    Jul 8, 2020 at 15:24

1 Answer 1

0
(JSON.parse(JSON.stringify(this.productarray))

will always return a string, so you're entering your if block too early. Rewrite your if as:

if(typeof this.shippingcharges == "number" && typeof this.salestax == "number" && this.productarray instanceOf Array) {
1
  • I made the change, also I am getting complete data in console.log, number of records passing in apex is correct, but value of all fields are null.
    – user75908
    Jul 10, 2020 at 15:57

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.