+ 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.
    public static object sumRollup(String recordObject,String recordField) {
        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)  {
            System.debug('Sum amount -- ' + recordSum);
        return recordSum;
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");
      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.