0

I've seen sfdcfox's answer to a similar question, yet am unable to replicate the result in my own component. I'd like to load a User's custom field via LDS with a User Id provided by my client controller, but am unable to successfully reload force:recordData so that it actually pulls the User's data. I'm sure it's something small, but I really can't find it and have sunk hours into this seemingly simple task. Any help is appreciated.

component

<aura:component implements="lightning:actionOverride,flexipage:availableForRecordHome,force:hasRecordId" access="global">
<aura:handler event="c:newEngagement" action="{!c.handleNewEngagement}"/>
<!--Account Record Id-->
<aura:attribute name="recordId" type="String"/>
<!--Current User Record Id-->
<aura:attribute name="userId" type="Id"/>
<aura:attribute name="thisUser" type="User" default="{}"/>
<aura:attribute name="errors" type="String"/>
<force:recordData aura:id="currentUser"
                   recordId = "{!v.userId}"
                   fields="Internal_Employee_Contact_ID__c, Id"
                   targetFields="{!v.thisUser}"
                   targetError="{!v.errors}"
                   mode="VIEW"/>

Controller

({
handleNewEngagement : function(component, event, helper) {

    component.set("{!v.userId}", $A.get("$SObjectType.CurrentUser.Id"));
    let thisUser = component.find("currentUser");
    thisUser.reloadRecord();
    console.log(component.get("{!v.thisUser.Id}")); // returns null
    console.log(component.get("{!v.thisUser.Internal_Employee_Contact_ID__c}")); // returns null
})

2 Answers 2

1

In Lightning Component javascript you should not use brackets and an exclamation point when referencing attributes. You JS should look something like this:

({
  handleNewEngagement : function(component, event, helper) {
    component.set("v.userId", $A.get("$SObjectType.CurrentUser.Id"));
    let thisUser = component.find("currentUser");
    thisUser.reloadRecord();
    console.log(component.get("v.thisUser.Id")); // returns null
    console.log(component.get("v.thisUser.Internal_Employee_Contact_ID__c")); // returns null
})

EDIT1:

Also referencing the answer you attached, sfdcfox places the reloadRecord() action in an extra asynchronous call via setTimeout. Have you tried doing this as well?

EDIT2: This is likely not the best way to do it but it might be worth a try.

<aura:attribute name="userId" type="Id"/>
<aura:attribute name="thisUser" type="User" default="{}"/>
<aura:attribute name="errors" type="String"/>

<aura:if isTrue="{!v.userId}">
    <force:recordData aura:id="currentUser"
                   recordId = "{!v.userId}"
                   fields="Internal_Employee_Contact_ID__c, Id"
                   targetFields="{!v.thisUser}"
                   targetError="{!v.errors}"
                   mode="VIEW"/>
</aura:if>

This way the force:recordData component wont load until you have filled in the userId attribute in your handler method. Also just try displaying the fields on the ui to see if they show up. Do something like:

Id : {!v.thisUser.Id}
Internal Emp Contact ID : {!v.thisUser.Internal_Employee_Contact_ID__c}
7
  • gave this a whirl, but still no luck. For what it's worth I've never had issues using bracketed references - but no brackets does look cleaner...I was thinking this had something to do with the structure of my force:recordData declaration. Feb 14, 2019 at 1:05
  • @NicholasZozaya Edited Feb 14, 2019 at 1:17
  • He does this because he's using returned data from a server side call to equip LDS with a record id - i'm simply attempting to reload on the client side without going to server side. So I don't think it's applicable. Feb 14, 2019 at 1:18
  • 1
    It seems to be to handle aura:valueChange which is client side. Feb 14, 2019 at 1:20
  • 1
    @NicholasZozaya check edit 2 Feb 14, 2019 at 1:36
0

As this issue was occurring on a component being surfaced on an account detail page, I solved for this by hooking my User force:recordData reloadRecord() to an Account force:recordData recordUpdated parameter.

Component

<aura:component implements="lightning:actionOverride,flexipage:availableForRecordHome,force:hasRecordId" access="global">
    <aura:handler event="c:newEngagement" action="{!c.handleNewEngagement}"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <!--Account Record-->
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="account" type="Object" default="{}"/>
    <aura:attribute name="accountErrors" type="String"/>
    <!--Current User Record-->
    <aura:attribute name="userId" type="Id" default=""/>
    <aura:attribute name="thisUser" type="Object" default="{}"/>
    <aura:attribute name="errors" type="String"/>
    <aura:attribute name="emplContactId" type="String"/>
    <force:recordData aura:id="currentAccount"
                      recordId = "{!v.recordId}"
                      fields="Id"
                      targetFields="{!v.account}"
                      targetError="{!v.accountErrors}"
                      recordUpdated="{!c.handleAccountUpdated}"
                      mode="VIEW"/>
    <force:recordData aura:id="currentUser"
                       recordId = "{!v.userId}"
                       fields="Internal_Employee_Contact_ID__c, Id"
                       targetFields="{!v.thisUser}"
                       targetError="{!v.errors}"
                       recordUpdated="{!c.handleUserUpdated}"
                       mode="VIEW"/>
</aura:component>

Controller

({

    doInit : function(component, event, helper){
        component.set("v.userId", $A.get("$SObjectType.CurrentUser.Id"));
    },

    handleAccountUpdated : function(component, event, helper){

        var eventParams = event.getParams();
        if(eventParams.changeType === "LOADED") {
            component.find("currentUser").reloadRecord();
        } else if(eventParams.changeType === "CHANGED") {
            // record is change
        } else if(eventParams.changeType === "REMOVED") {
            // record is deleted
        } else if(eventParams.changeType === "ERROR") {
            console.log('error');
            // there’s an error while loading, saving, or deleting the record
        }
    },

    handleUserUpdated : function(component, event, helper){

        var eventParams = event.getParams();
        if(eventParams.changeType === "LOADED") {
            component.set("v.emplContactId", component.get("v.thisUser.Internal_Employee_Contact_ID__c"));
        } else if(eventParams.changeType === "CHANGED") {
            // record is change
        } else if(eventParams.changeType === "REMOVED") {
            // record is deleted
        } else if(eventParams.changeType === "ERROR") {
            console.log('error');
            // there’s an error while loading, saving, or deleting the record
        }
    }

You must log in to answer this question.

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