6

I've run into an issue with returning data from Apex to LWC which I'm having trouble figuring out.

I've set up an LWC which calls an apex method to make a callout an return the result of said callout after some server-side processing has been made. On server side I save the processed data in an object "CreditDecision" which also is the method return type. I then pass back to the LWC as the response.

However, In the LWC, the response from the method call is blank. When i try to just send a string as a response the result is returned just fine.

Is this due to LWC just being able to recieve sObjects, List of sObjects and primitive datatypes as returned data, or am I missing something?

Heres the code:

Returned Apex Object

   public class CreditDecision {
    Account account {get;set;}
    Opportunity opportunity {get;set;}
    
    Boolean creditAuthorized {get;set;}
    Decimal appliedCredit {get;set;}
    Decimal approvedCredit {get;set;}
    String creditRating {get;set;}
    String decisionDesc {get;set;}
    String dateOrdered {get;set;}
    String error {get;set;}
}

And the LWC code

            doCreditCheck({recordId: this.recordId,sObjectType: this.sObjectName,appliedCredit: this.appliedCredit})
        .then(result => {

            //For some reason the Result is empty {} when result isn't a primitive or sObject

            const decision = result;
            console.log('result: ' + JSON.stringify(result));
            console.log('Result data: ' + result.data);
            console.log(decision);
            this.isLoading = false;
            this.creditCheckCompleted = true;

            if(decision === 'K') {
                this.creditCheckControll = true;
            }else if (decision === 'G') { 
                this.creditCheckApproved = true;
            }else if (decision === 'A') {
                this.creditCheckDeclined = true;
            }
1
  • It's empty because you haven't added @auraEnabled attribute to any of the members. Commented Dec 18, 2020 at 6:13

2 Answers 2

22

Make members public and @auraEnabled to make them available in the lightning component.

public class CreditDecision {
    @auraEnabled
    public Account account {get;set;}
    @auraEnabled
    public Opportunity opportunity {get;set;}
    @auraEnabled
    public Boolean creditAuthorized {get;set;}

    // and so on 
}
1
  • 3
    Awesome, that did it. Thanks alot Rahul!
    – Cnork
    Commented Dec 18, 2020 at 7:17
1

I'm unable to leave a comment on Rahul's response, but felt this was important to share in addition to his answer:
Even if the wrapper you are using is not being directly referenced and the wrapped data is deeply nested, the @AuraEnabled decorators still matter!!!
For instance, I have an LWC making an imperative call to an Apex controller, which passes a Map<String,Object> to an Apex helper, which retrieves data and passes it through a wrapper before passing back to the controller, passing it back to the LWC. The wrapped response is also Map<String, Object>, but the Object contains a key with a value that is a List, which in turn contains a key with a value that is a List. In this scenario, every single object class that is defined in the wrapper must be public and have each of its members @AuraEnabled. Otherwise, the call will succeed but the Objects will be empty. I hope this helps someone because it sure drove me nuts!

You must log in to answer this question.

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