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
ankethaanketha 

Trying to show a count of a sobject on the page with lightning web component

I'm trying to pass a parameter which is an account field to a soql query(which is just calculating the count on this custom object based on the account field). And I'm trying to retrieve this count value from the controller to the js file. And from the js file to the HTML Page.

But the challenge here is that all the documentation on this shows examples on a list of Sobjects. And even though this can be done on a single sObject through getSobjectValue, this method doesn't work by passing any parameter.
Below is my code and explanation of what I'm trying to do--

public class LWLeaseCountExtensionsController { @AuraEnabled(cacheable=true)
public static List<Account> getAccount(String AccId) {
return [SELECT Id,LeaseWaveID__c, LW_Customer_ID__c, LW_Fleet_ID__c, LW_Account_ID__c, LW_SubAccount_ID__c from Account where Id=:AccId Limit 1]; }

@AuraEnabled() public integer lc;

/*This was the original code that I tried first, but since it didn't work with a return type of Integer, I commented out the below method and created the next method which take a list of Integer as return type */ @AuraEnabled(cacheable=true)
public static Integer getLeaseCount(string LeaseWaveID) {
Integer lc = !string.isblank(LeaseWaveID) ? [SELECT COUNT() FROM DW_oData_vw_sf_Units__x WHERE Lease__c = TRUE AND LeasewaveId__c =:LeaseWaveID] : -1;

@AuraEnabled(cacheable=true)
public static List<Integer> getLeaseCount(string LeaseWaveID) { List<integer> lc;
Integer i;
if(LeaseWaveID!=null)
       i=[SELECT COUNT() FROM DW_oData_vw_sf_Units__x         WHERE Lease__c = TRUE AND LeasewaveId__c =:LeaseWaveID];
else
      i=-1;
lc.add(i);
system.debug('leaseWaveID1--'+lc);
return lc;
}
}

js file --
import { LightningElement,api,wire,track } from 'lwc';
//import { getSObjectValue } from '@salesforce/apex';
import getAccount from '@salesforce/apex/LWLeaseCountExtensionsController.getAccount'; import Account_LeaseWaveId from '@salesforce/schema/Account.LeaseWaveID__c';
import getLeaseCount from '@salesforce/apex/LWLeaseCountExtensionsController.getLeaseCount';

export default class TestLWLeaseCountwebComponent extends LightningElement {
@api recordId;
@track getLeaseCount;
@api totalLC;

@wire(getAccount,{AccId:'$recordId' })
accounts;

@wire(getLeaseCount, { LeaseWaveID: Account_LeaseWaveId}) odata;

totalLC = JSON.stringify(this.lc); }


Current HTML File which doesn't work --
<template>
    <lightning-card title="LWCount" icon-name="custom:custom63">
         <div class="boarder">
              <!-- <p class=”slds-m-bottom_small”>
                          <lightning-button label=”LoadContacts” onclick= {handleLoad}></lightning-button>
                </p> -->

        <template if:true={accounts.data}>
             {LeaseWaveID} </template>
             {totalLC}

          <template if:true={odata.data}></template>
       </div>
    </lightning-card>
</template>

When I set the debug Logs to check if the method getLeaseCount is getting called when the Account page(which has this component) loads, this method is not getting called. Only the getAccount method gets called and the value is being retrieved.

The below HTML Works--
<template>
   <div class="boarder">
      <template if:true={accounts.data}>
          <ui>
              <template for:each={accounts.data} for:item="acc">
                 <li key={acc.Id}>{acc.LeaseWaveID__c}</li>
              </template>
            </ui>
          </template>
      </div>
</template>

Though the above HTML Works and displays an account field value, that doesn't serve my purpose as I want the count() of the custom object to be displayed through the html and I can't figure out a way to display the count.

Can anyone please help me with this. I'm referring to the section (Update code to pass record Id to Apex controller ) in the below documentation : 
https://rajvakati.com/2018/12/29/get-record-id-into-lightning-web-components

Regards,
Anketha