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