You need to sign in to do that
Don't have an account?
BrianWX
How to Use AggregateResult in LWC
I built a custom class and method to return the aggregate data to plot a chart using LWC. For the purpose of example, I hardcode the AccountId for now.
Here's my JS file. How do I retrieve the aggregate values from the custom apex controller and use in the JS? I tried getSObjectValue, but that did not work as it worked only with field data from the return Object.
Here's the sample of the HTML file
public with sharing class lwcLifetimeSpendingController { @AuraEnabled (cacheable=true) public static List<AggregateResult> getAccountLifetimeSpending(){ return [SELECT SUM(Service_Amount__c) Service, SUM(Subscription_Booking__c) Subscription FROM Opportunity WHERE accountid = '0018000001G9dZUAAZ' and isclosed=true and iswon=true]; } }
Here's my JS file. How do I retrieve the aggregate values from the custom apex controller and use in the JS? I tried getSObjectValue, but that did not work as it worked only with field data from the return Object.
import { LightningElement,track,wire } from 'lwc'; import { getSObjectValue } from '@salesforce/apex'; import { loadScript } from 'lightning/platformResourceLoader'; import chartjs from '@salesforce/resourceUrl/chart'; import getAccountLifetimeSpending from '@salesforce/apex/lwcLifetimeSpendingController.getAccountLifetimeSpending'; const generateRandomNumber = () => { return Math.round(Math.random() * 100); }; export default class LifetimeSpending extends LightningElement { @wire(getAccountLifetimeSpending) lifetimeAggregateResult; @track error; //getSObjectValue does not work in this scenario } //end export
Here's the sample of the HTML file
<template> <lightning-card title="Lifetime Spending" icon-name="custom:custom19"> <div class="slds-m-around_medium"> <canvas class="donut" lwc:dom="manual"></canvas> </div> <template if:true={error}> <c-error-panel errors={error}></c-error-panel> </template> </lightning-card> </template>
property Or Function after @wire.
@wire(getAccountLifetimeSpending) lifetimeAggregateResult; // property
or function: wiredAccountLifetimeSpending({ error, data })
https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex
import apexMethod from '@salesforce/apex/Namespace.Classname.apexMethod';
@wire(apexMethod, { apexMethodParams })
propertyOrFunction;
I updated using the @wire(apexMethod, {apexMethodParams}) it was able to retrieve data and assign data to local var.
One thing I could not figure out why it could not get data from the local var in the code below. It could not render the values for the line data: [this.serviceAmount, this.subscriptionAmount]
console.log(JSON.stringify(data)); helps above all to verify the content
but you can use directly: this.lifetimeAggResult = data; very likely.
this.lifetimeAggResult = data;
this.serviceAmount = this lifetimeAggResult[0].Service;
this.subscriptionAmount = this.lifetimeAggResult[0].Subscription;
Dynamic Chart in LWC With Multiple Datasets Using ChartJS (https://salesforcerealm.com/2020/12/09/chart-in-lwc-using-chartjs/)