0

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?

1 Answer 1

1

You seemed to have gotten your value providers mixed up:

  • c = A component’s controller, which enables you to wire up event handlers and actions for the component
  • v = A component’s attribute set. This value provider enables you to access the value of a component’s attribute in the component’s markup.

You appropriately use c.fetchUser to use the controller's method to take an action, but you don't want to do the same when accessing your defined attributes.

initUIKit: function(component, message, helper) {
    //this doesn't work
    var userId = component.get("c.userInfo").My_Custom_Field__c;
    
    //should be this
    var userId = component.get("v.userInfo").My_Custom_Field__c;
    ...
}


You also, technically, don't need apex. You could leverage force:recordData combined with setting the appropriate current userId in an attribute during your init

<aura:attribute name="userRecord" type="Object"/>

<aura:if isTrue="{!v.userId}">
    <force:recordData 
        aura:id="recordLoader"
        recordId="{!v.userId}"
        fields="My_Custom_Field__c"
        targetFields="{!v.userRecord}"
        targetError="{!v.recordLoadError}"
    />
</aura:if>

Then, when needed, you can get the user record and access the field you loaded with it.

let userCustomField = component.get("v.userRecord").My_Custom_Field__c;

You must log in to answer this question.

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