3

To use my Lightning component for record pages of multiple types I need to load the record in a generic fashion.

<aura:component controller="MyCtrl" implements="force:hasRecordId,force:hasSObjectName,...">
    <aura:attribute name="record" type="Map" />

    <force:recordData recordId="{!v.recordId}" targetRecord="{!v.record}"
                      fields="{!(v.SObjectName == 'NS__MyObject__c') ? 'NS__lkp_Parent__c' : 'Id'}" />
    ...
</aura:component>

Not all of those type have a specific field called NS__lkp_Parent__c. Lightning Data Service refused (fail) to load the whole record when I pass non-existing fields (which is bad!). Therefor I added a condition that checks the SObjectName before.

But this also fails with a generic error:

Unfortunately, there was a problem... Action failed: forceChatter:lightningComponent$controller$doInit [Error while creating content for lightning component quick action] quickActionHandlerHelper.js failed to create component - forceChatter:lightningComponent

What am I doing wrong?

1

I believe that your code is failing because you are returning a string value instead of an array of strings in the fields attribute (either 'NS__lkp_Parent__c' or 'Id').

Edited: looks like expressions cannot return an array in this case so the solution is to set the fields value via JS instead of using an expression.

Here are the steps:

1) add an aura:id="lds" attribute to the force:recordData component

2) add an init handler to your parent component

3) set the fields attribute in your init handler:

component.find('lds').set('v.fields', yourFieldsArray);
5
  • This line of code doesn't save. My IDE tells me that the square brackets for the String arrays don't work in this place. :-( – Robert Sösemann Jul 18 '18 at 9:01
  • 1
    Ok, in that case, you will need to assign the fields value programmatically instead of using an expression. I will update my answer. – POZ Jul 18 '18 at 9:10
  • Although you brought the solution we also added our code as a separate answer. – Robert Sösemann Jul 18 '18 at 9:13
  • 1
    ah just saw your comment too late :( – POZ Jul 18 '18 at 9:17
  • 1
    Looks like I also do need to set a valid fields default like ['Id']. Otherwise the page doesn't load. – Robert Sösemann Jul 18 '18 at 9:21
1

Adding the conditional directly into the fields attribute didn't work as the square brackets after the ? was not allowed to be saved.

Nonetheless the root cause mentioned by POZ was the solution. fields is not a comma-separated string but a String array of type String[].

Here is how we changed our code:

Component:

<aura:component controller="MyCtrl" implements="force:hasRecordId,force:hasSObjectName,...">
    <aura:attribute name="record" type="Map" />
    <aura:attribute name="recordFields" type="String[]" />

    <aura:if isTrue="{!v.recordFields.length > 0}">
        <force:recordData recordId="{!v.recordId}" targetFields ="{!v.record}" fields="{!v.recordFields}" />
    </aura:if>

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

Controller:

({
    onInit: function(cmp) {
        let fields = ['Id'];

        if(cmp.get("v.sObjectName") !== 'NS__MyObject__c') {
            fields.push('NS__lkp_Parent__c');
        }

        cmp.set("v.recordFields", fields);
    },
});

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.