0

This question already has an answer here:

Having Issues with getting the returned string from the apex controller. If I use getState and check if success it wont run and will only run if there is an error in the state. Just from a glance anyone have a recommendation? I'm just trying to render the record Owner's fullphotourl and this component is an iterated component.

Component:

<aura:component implements="force:hasRecordId" access="global" controller="MyChecklistExperiment">
   <aura:attribute name="checklistId" type="Id" />
   <aura:attribute name="targetFields" type="Checklist_Team__c" />
   <aura:attribute name="showDialog" type="String" />
   <aura:attribute name="remoteRecordId" type="Id"/>
   <aura:attribute name="SmallPhotoUrl" type="String" />

   <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> 

  <force:recordData aura:id="checlistTeamRecord"
                  recordId="{!v.checklistId}"
                  targetFields="{!v.targetFields}"
                  fields="Id, Name, Role__c, Owner.Name, Owner.Email, OwnerId, Member__c, Member__r.Name"
                  />
    <div class="slds-media__figure">
        <lightning:avatar src="{!v.SmallPhotoUrl}" fallbackIconName="standard:user" class="slds-avatar--large slds-avatar--circle"/>          
    </div>

Javascript:

    ({
doInit : function (component, event, helper){
    var action = component.get("c.getRelatedOwner");
    action.setParams({
        userId: component.get("v.targetFields.OwnerId")
    });
    action.setCallback(this, function(response) {
        var url = response.getReturnValue();
        component.set("v.SmallPhotoUrl", url);
    });
    $A.enqueueAction(action);
},        

Apex Controller:

   public with sharing class MyChecklistExperiment {

@AuraEnabled
public static String getRelatedOwner(Id userId){

    String test;
    test = [
            SELECT Id, fullPhotoUrl
            FROM User
            WHERE Id =:userId
        ].fullPhotoUrl;
    system.debug(test);
    return test;
   }
 }

marked as duplicate by sfdcfox, glls, Santanu Boral, Tushar Sharma, martin Sep 14 '17 at 6:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Have you tried console.log(response.getError()[0].message) in your callback. Then check your debug logs in your browser developer tools. – gNerb Sep 13 '17 at 21:21
  • Toby great point. Will try that and update if I see anything. Thank you for your suggestion. – Michael Yocca Sep 13 '17 at 21:39
  • I'm getting undefined in my callback. – Michael Yocca Sep 13 '17 at 21:55
3

force:recordData loads its data asynchronously. This means the data you want is not available in the init method. Set a handler on your targetFields, and then listen for a change to that, instead.


<aura:handler name="change" value="{!v.targetFields}" action="{!c.getOwnerData}"/> 

({
  getOwnerData: function (component, event, helper){
    var action = component.get("c.getRelatedOwner");
    action.setParams({
      userId: component.get("v.targetFields.OwnerId")
    });
    action.setCallback(this, function(response) {
      var url = response.getReturnValue();
      component.set("v.SmallPhotoUrl", url);
    });
    $A.enqueueAction(action);
  },
  • sfdcfox Bravo. Worked like a champ. That makes perfect sense now. Thank you! – Michael Yocca Sep 13 '17 at 22:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.