function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ridhima Saxena 21Ridhima Saxena 21 

I want to calculate sum of fields dynamically in lwc??

Hii Team,

As I have written one apex method for sum rollup functionality which is working fine when i call from anonymous window.
                                                 Apex
@AuraEnabled
    public static object sumRollup(String recordObject,String recordField) {
        System.debug(recordObject);
        Object recordSum;             
        //String sum = [select sum(annualrevenue) from account];
        String sum = 'select' +' '+ 'SUM'+ '('+recordField+')'  + ' from ' + recordObject;  
        AggregateResult[] groupedResults = database.query(sum);
        for (AggregateResult ar : groupedResults)  {
            recordSum=ar.get('expr0');         
            System.debug('Sum amount -- ' + recordSum);
        }
        return recordSum;
    } 
But 
when i select field from picklist dynamically it is not pritting result only run till console.log("2 sum") You can see this console below.
                                              Here is my JS
sumRecordType() {
    console.log("2 sum");
    sumRollup({
      recordObject: this.selectedObjectValue,
      recordField: this.selectedFieldValue,
    })
      .then((result) => {
        console.log("Hello Sum" + result);
        this.recordCountTypeValue = result;
      })
      .catch((error) => {
        this.error = error;
      });
  }
How to resolve it Please help This is LWC question
Saravana Bharathi 1Saravana Bharathi 1
Hi Ridhima,

sumRollup is the auraenabled function, so it has to imported in lwc. 

If its not imported, sumRollup cannot be recognized in javascript layer. Check the below code in js file.

in JS file, 
import sumRollup from '@salesforce/apex/<Apexclassname>.sumRollup';

If it solves the problem, mark it as resolved.

Thanks