0

I've got an LWC component that calls an Apex method that creates an event, sends an email, and then returns a custom DTO object. The Apex method works with my test class but when I call it through the LWC, it always return an empty object.

Heres my LWC:

import { LightningElement, api } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import  otJs from '@salesforce/resourceUrl/opentok';
import { loadScript } from 'lightning/platformResourceLoader';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import OpenTok from '@salesforce/apex/OpenTokViewController.sendNewSessionDetails';

export default class VideoChatLinks extends LightningElement {
    session;
    @api recordId;
    
    renderedCallback() {
        // ....
        
    }

    handleError(error) {
   // .......
    }
    
    videoBySMS() {
        console.log('videoBySMS clicked');
        this.getOTSession('sms');
    }

    videoByEmail() {
        console.log('videoByEmail clicked');
        this.getOTSession('email');
    }

    getOTSession(via) {
        OpenTok({via: via, contactId: this.recordId})
        .then((data) => {
            if (data) { **// data is always {}**
               this.session = data;
               if (this.session && this.session.sessionId) { **//This always false b/c data is {} **
                   this.initializeSession(this.session.apiKey, this.session.sessionId);
               } else {
                   this.handleError('Session data could not be retrieved');
               }
             } else {
                this.handleError('Session not created. Check the logs for details');
             }
        })
        .catch(error => {
            this.handleError(error);
        });
    }

    initializeSession(apiKey, sessionId) {
       // .....
    }
}

The Apex code :

public class OpenTokViewController {
  @AuraEnabled()
  public static OpenTokSessionDTO sendNewSessionDetails(String via, Id contactId) {
    //create an OpenTokSession and return the data needed.
    return new OpenTokSessionDTO(session.sessionId, session.apiKey);
  }
}

public with sharing class OpenTokSessionDTO {
    public String sessionId { get; set; }
    public Integer apiKey { get; set; }
    public OpenTokSessionDTO(String sessionId, Integer apiKey) {
        this.sessionId = sessionId;
        this.apiKey = apiKey;
    }
}

1 Answer 1

1

Since OpenTokSessionDTO is a custom class, you will need to make its properties accessible and serializable by the Aura/Lightning framework:

public with sharing class OpenTokSessionDTO {

    @AuraEnabled 
    public String sessionId { get; set; }
    @AuraEnabled 
    public Integer apiKey { get; set; }

    public OpenTokSessionDTO(String sessionId, Integer apiKey) {
        this.sessionId = sessionId;
        this.apiKey = apiKey;
    }
}
2
  • thanks @nbrown. That did it!
    – Seth King
    Commented Oct 8, 2020 at 19:24
  • @SethKing Be sure to check the ✔ so people know the answer solved your problem (and you'll get bonus points, too!).
    – sfdcfox
    Commented Oct 8, 2020 at 21:11

You must log in to answer this question.

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