[SalesForce] Return an Instance of a Class in a LWC (Lightning Web Component)

Below I am Trying to Return an Instance of a Class to a LWC JS to then handle and display to the End User. I am able to successfully call the Apex Class and Method and return data, but the data is empty/Null. The actual Apex Class/Method Call is done towards to bottom of the JS.

Here is my LWC JS:

import {LightningElement, wire, api, track} from 'lwc';
import getActivityAndNotes from '@salesforce/apex/viewAllActivityNotesOnAccountLEX.getActivityAndNotes'
import {getRecord, getFieldValue, getFieldDisplayValue} from 'lightning/uiRecordApi';
import ACC_ID from '@salesforce/schema/Opportunity.AccountId';

// Define Variables
var name;
var accountId;

export default class TestActivityPayload extends LightningElement {

    // Variables
    @api recordId;
    @track accId;

    name;
    accountId;

    @wire(getRecord, { recordId: '$recordId', fields: ACC_ID})
    account({ error, data}) {
        if (data) {
            console.log('Request #1 - Data:');
            console.log(data);

            this.accId = getFieldValue(data, ACC_ID);
            console.log(this.accId);
            accountId = this.accId;
            console.log(accountId); 

        } else if (error) {

            console.log('Request #1 - Error:');
            console.log(error);

        } else {

            console.log("Request #1 - Nothing was returned")

        }
    }

    @wire(getActivityAndNotes, {accountId: '0015C00000PBQmvQAH'})
    returnedData({error,data}) {
        if (data) {

            console.log('Request #2 - Data:');
            console.log(data);
            console.log('Request #2 - Data.Activities:')
            console.log(data.Activities);
            console.log('Request #2 - Data.Notes:');
            console.log(data.Notes);
            console.log("THIS:");
            console.log(this);

        } else if (error) {

            console.log('Request #2 -Error:');
            console.log(error);

        } else {

            console.log("Request #2 - Nothing was Returned")

        }
    }

}

The goal here is to first get the Account Id from the Opportunity that the User is on and pass that value to my Apex Method and Class which is called 'getActivityAndNotes'. For the sake of testing I hard coded an Account Id into the Apex Call to ensure that my declaration of the variable was not the cause of the issue. I am able to see that Data is returned from the call but there is nothing in it.

Here is an example of what is being returned:
enter image description here

Here is the Apex Class that I created and is being called:

public class viewAllActivityNotesOnAccountLEX {
    @AuraEnabled(cacheable=true)
    public static ActivityAndNotes getActivityAndNotes(string accountId) {

        ActivityAndNotesHelper activityAndNotesHelper = new ActivityAndNotesHelper();
        ActivityAndNotes activityAndNotes = activityAndNotesHelper.GetActivityAndNotesOnAccount(accountId);

        return activityAndNotes;
    }
}

As you can see I declared the class '@AuraEnabled'.

Here is the 'ActivityAndNotes' Class:

public class ActivityAndNotes {
    public List<wrapActivity> Activities { get; set; }
    public List<wrapNotes> Notes {get; set; }
}

I am currently unsure if there is something wrong with the way I am calling the Apex Method/Class or if there is a problem with the Apex. I have successfully been able to return data but not in the this format. I have been able to return data from Apex Classes/Methods Such as below:

NOT RELEVANT TO THE CURRENT ISSUE:

public with sharing class RelatedOpportunities {
    @AuraEnabled(cacheable=true)
    public static List<Opportunity> getOpportunityList(string recordId) {
        String record = recordId;
        return [
            SELECT Id, Name, StageName, Amount, Type, RecordTypeId
            FROM Opportunity
            WHERE AccountId =:record AND IsClosed = FALSE
        ];
    }
}

Any direction or assistance that could be provided would be greatly appreciated.

  1. Is there an issue with the way I am calling the Apex Method/Class?
  2. Is there an issue with my Apex Method/Class? Do I have to return data differently? Is something not declared correctly?
  3. Is this currently possible with a LWC or do I have to change my overall approach?

Best Answer

Is this currently possible with a LWC or do I have to change my overall approach?

You just need a minor tweak.

Is there an issue with the way I am calling the Apex Method/Class?

Nope.

Is there an issue with my Apex Method/Class? Do I have to return data differently? Is something not declared correctly?

Only @AuraEnabled properties are serialized. You need to fix your class.

public class ActivityAndNotes {
    @AuraEnabled public List<wrapActivity> Activities;
    @AuraEnabled public List<wrapNotes> Notes;
}

The { get; set; } is not necessary for LWC/Aura serialization.

Related Topic