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
Claire CrabtreeClaire Crabtree 

Returning a primitive data type from an apex class using @wire?

Hello, I am an admin adventuring into learning code and I cannot figure out why the following Lightning Web Component is not working. (I've left out some of the code for simplicity.) Thank you!

HTML:
<p>Total Licenses:<span class="slds-p-horizontal_small">{totalPartnerLicenses}</span></p>

JS:
import { LightningElement, track, wire } from 'lwc';
import queryTotalPartnerLicenses from '@salesforce/apex/queryForLicenses.queryTotalPartnerLicenses';
export default class PartnerCommunityLicenses extends LightningElement {
@track totalPartnerLicenses = 7;
@wire(queryTotalPartnerLicenses) totalPartnerLicenses;
}

APEX CLASS:
public with sharing class queryForLicenses {
@AuraEnabled(cacheable=true)
public static Integer queryTotalPartnerLicenses(){
List<UserLicense> totalPL = [SELECT ID, TotalLicenses FROM UserLicense WHERE Name = 'Partner Community'];
UserLicense pl = new UserLicense();
Integer numTotalLicenses;

if(totalPl.size()>0){
pl = totalPL[0];
numTotalLicenses = Integer.valueOf(pl.TotalLicenses);
}
else {
numTotalLicenses=0;
}
return numTotalLicenses;
}
}
Best Answer chosen by Claire Crabtree
Maharajan CMaharajan C
Hi Claire,

Try the below changes it will work:

While you wire the property you have to use the data to access your apex return data.


HTML:
<p>Total Licenses:<span class="slds-p-horizontal_small">{totalPartnerLicenses.data}</span></p>

JS:
import { LightningElement, wire } from 'lwc';
import queryTotalPartnerLicenses from '@salesforce/apex/queryForLicenses.queryTotalPartnerLicenses';
export default class PartnerCommunityLicenses extends LightningElement {
@wire(queryTotalPartnerLicenses) totalPartnerLicenses;
}

Thanks,
Maharajan.C