Returned APEX object empty in LWC

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;
            }

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Make members public and @auraEnabled, then they will be 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 
}

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply