7

I am using Lightning Data Services to have the current record already prefetched

<aura:component controller="MyCtrl" implements="force:hasRecordId">
    <aura:attribute name="parent" type="SObject" />

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

    <force:recordData aura:id="recordLoader"
                      recordId="{!v.recordId}"
                      fields="Name,..."
                      targetRecord="{!v.parent}"
                      layoutType="FULL" />

   {!v.market.fields.Name.value} <-- WORKS!
</aura:component>

I can directly display its fields in the component without any problems. But when I pass the record

({
    init: function(cmp, evt, helper) {
        var action = cmp.get("c.queryData");
        action.setParams({ parent: cmp.get("v.parent") });
        action.setCallback(this, function(response) {
            ...
        });

        $A.enqueueAction(action);   
    },
})

into my Apex init method fields are not accessible anymore

public class MyCtrl {

    @AuraEnabled
    public static String queryData(Parent__c parent) {
         // parent is null
    }
}
7

force:recordData loads the data asynchronously. This means it won't be available in the init method. Instead, listen for a change event and then get your data:

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

loadChildData: function(component, event, helper) {
    var action = component.get("c.queryData");
    action.setParams({ parent: component.get("v.parent") });
    action.setCallback(...);
    $A.enqueueAction(action);
}

The layout for the force:recordData object doesn't align well with the default layout, and the Locker Service Proxy tends to muck things up. Here's an example I finally got working in my org:

change: function(component, event, helper) {
    var action = component.get("c.queryData"), account = component.get("v.account"), param;
    param = { sobjectType: 'Account', Id: account.fields.Id.value, Name: account.fields.Name.value };
    action.setParams({ record: param });
    console.log(param);
    action.setCallback(this, function(result) { console.log(result.getReturnValue()); });
    $A.enqueueAction(action);
}
  • Good point. Thanks. But replacing my init with a change handler doesn't change anything. – Robert Sösemann Aug 14 '17 at 12:50
  • 2
    @RobertSösemann This worked in my dev org. Make sure everything matches up namewise. – sfdcfox Aug 14 '17 at 12:58
  • 2
    @RobertSösemann It appears nearly instantly (~1/10th of a second, it looks like). However, nearly instantly is not soon enough for init to see the data. – sfdcfox Aug 14 '17 at 13:12
  • 1
    @RobertSösemann For some Locker Service-related reason, you apparently can't just pass the object verbatim into a parameter, but instead need to construct a new object to pass in. – sfdcfox Aug 14 '17 at 13:54
  • 1
    @RobertSösemann As you wish. – sfdcfox Aug 14 '17 at 14:55
3

Okay! So Here is what you are Missing here. 1. you can not send Sobject as parameter so break it into fields that you want to use. 2.You are not using recordUpdated="{!c.doInit}" in force:recoedData That is why init works for first time and it gets null. I created custom object Property and did this. Take a look

Component :

<aura:component controller="MyCtrl" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >

<aura:attribute name="property" type="Property__c" />

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

 <force:recordData aura:id="propertyService" 
                  recordId="{!v.recordId}"
                  targetRecord="{!v.property}"
                  recordUpdated="{!c.doInit}"
                  layoutType="FULL" />

 </aura:component>

jsController : which has a custom field named as beds__c

({
doInit : function(component, event, helper) {
   var action = component.get("c.queryData");        
   action.setParams({
        beds: component.get("v.property.fields.Beds__c.value")
   });
    action.setCallback(this, function(response) {

    });

    $A.enqueueAction(action);   
}
})

Apex Controller :

public class MyCtrl {

@AuraEnabled
public static void queryData(String prop) {

    System.debug('parent' + prop);
     // prop is not null
}
}
0

Seems Like There is no method name queryData in your apex controller and The parameters name is also not same.

  • I That was just an error I introduced when I prepared my code for SFSE. I corrected the question. – Robert Sösemann Aug 14 '17 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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