0

I'm trying to receive some data on the frontend using this Apex class (see below). The returned data however is an empty error. I followed the official SF documentation, made everything public and decorated with @auraEnabled, but still no data. Am I missing something? Thanks in advance!

public with sharing class AFF_Score_class {
@AuraEnabled(cacheable=true)

public static list<Overview__c> bindScore(string accountId, string programQ){
    string Account = accountId;
    string Program = programQ;

    return [
        SELECT Id, Period__c, Name, Status__c, TotalEarned__c, EarnedThisYear__c
        FROM overview__c
        WHERE programme__c = :Program
        AND account__c = :Account
    ];
}

This is the code in my js file:

@wire(bindScore) score({data, error}){
debugger;
if(data){
    console.log ("yes data")
    console.log (data)
} 
else {
    console.log ("no data")
}

XML:

    <targetConfig targets="lightningCommunity__Default">
    <property name="accountId" type="String" />
    <property name="programQ" type="String" />
</targetConfig>

1 Answer 1

1

The normal way to pass data values from an LWC to an @AuraEnabled method is with a second parameter to the wire call supplying values using the same names that are used in the Apex e.g.:

recordId;

@wire(bindScore, {accountId: recordId, programQ: 'Program Xyz'})

A common pattern is also to reference the property of the component recordId using this special syntax:

recordId;

@wire(bindScore, {accountId: '$recordId', programQ: 'Program Xyz'})

as that ensures that the wire call is made automatically anytime that value changes.

See the various examples here Understand the Wire Service.

1
  • Thanks, this solved it for me. I added to the wire function{recordId: "$accountId", programPeriod: '$programPeriod'}
    – M.E.Amrani
    Mar 17, 2022 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .