I'm trying to create a Lightning Component to integrate Sendbird into Salesforce by following this guide: https://sendbird.com/developer/tutorials/how-to-integrate-sendbird-with-salesforce
I've successfully created this, and it does work using the Salesforce User ID as the Sendbird ID, but ideally I'd like to pass in a custom field from the User record instead. I know that I need an Apex controller for this, and I tried copying from this answer here but I can't seem to get it to work properly. It just comes back totally blank. I also tried adding the doInit
code directly in the handleMessage
method, and that doesn't work either.
Component:
<aura:component controller="CurrentUserInfoController" implements="flexipage:availableForAllPageTypes" access="global">
<lightning:utilityBarAPI aura:id="utilitybar" />
<aura:handler name="init" value="this" action="{!c.doInit}" />
<aura:attribute name="userInfo" type="User" />
<aura:attribute name="recordId" type="String" />
<aura:attribute name="data" type="String" />
<lightning:navigation aura:id="navService" />
<lightning:container
aura:id="SendBird_Bar"
src="{!$Resource.sendbird_bar + '/index.html'}"
onmessage="{!c.handleMessage}"
/>
</aura:component>
Controller:
({
doInit: function(component, event, helper){
var action = component.get("c.fetchUser");
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
var storeResponse = response.getReturnValue();
component.set("v.userInfo",storeResponse);
}
});
$A.enqueueAction(action);
},
handleMessage: function(component, message, helper) {
var payload = message.getParams().payload;
if (payload === "READY") helper.initUIKit(component, message, helper);
}
});
Helper:
({
initUIKit: function(component, message, helper) {
var userId = $A.get("$SObjectType.CurrentUser.Id"); //this works
//var userId = component.get("c.userInfo").My_Custom_Field__c; //this doesn't
console.log(userId);
var message = { userId: userId };
try {
component.find("SendBird_Bar").message(message);
} catch (err) {
console.error("Error from Utilities Bar:", err);
}
}
})
Apex Controller:
public class CurrentUserInfoController {
@AuraEnabled
public static User fetchUser(){
User u = [SELECT Id, Name, My_Custom_Field__c FROM User WHERE Id = :userInfo.getUserId()];
return u;
}
}
How do I fetch the User info and pass my custom field to my helper function?