2

Example:

I have this attribute, and I would be setting the field API value in DESIGN attribute in the app builder.

 <aura:attribute name="requiredFields" type="String[]" default="Student__r.Student__r.btydev__Picture_Id__c"/>
<aura:attribute name="requiredField" type="String" default="Student__r.Student__r.btydev__Picture_Id__c"/>

Then I have force:recordData

<force:recordData aura:id="internshipOriginalRecord"
                      recordId="{!v.recordId}"
                      targetRecord="{!v.record}"
                      targetFields="{!v.simpleRecord}"
                      fields="{!v.requiredFields}"
                      />

How to retrieve and print the field value?

This works fine:

{!v.simpleRecord.Student__r.Student__r.btydev__Picture_Id__c}

But, I want to pass the fieldAPI from the attribute dynamically. Something like this and get the value.

{!(v.simpleRecord)+'.'+ (v.requiredFields)}

Component:

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes">
    <aura:attribute name="recordId" type="String" />
    <aura:attribute name="record" type="Object" />
    <aura:attribute name="simpleRecord" type="Object" />    
    <aura:attribute name="recordLoadError" type="String"/>
    <aura:attribute name="requiredFields" type="String[]" default="Student__r.Student__r.btydev__Picture_Id__c"/>    
    <aura:attribute name="requiredField" type="String" default="Student__r.Student__r.btydev__Picture_Id__c"/>
    <aura:attribute name="reqdField" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

    <force:recordData aura:id="internshipOriginalRecord"
                      recordId="{!v.recordId}"
                      targetRecord="{!v.record}"
                      targetFields="{!v.simpleRecord}"
                      fields="{!v.requiredFields}"
                      targetError="{!v.recordLoadError}"
                      />

    <div >  
        <lightning:card iconName="standard:user" title="Student Photo">
            <div class="slds-p-horizontal--small">   
                <div class="slds-grid">
                    <div class="slds-col slds-size_1-of-2"> 
                        <aura:if isTrue="{!not(empty(v.simpleRecord.Student__r.Student__r.btydev__Picture_Id__c))}">
                            <div class="slds-form-element__control">
                                <img class="mythumbnail" src="{!'/servlet/servlet.FileDownload?file='+v.reqdField}"/>
                            </div>
                            <aura:set attribute="else">
                                <div class="slds-form-element__control">
                                <img src="{!$Resource.stdPhotoAvtar}"/>
                            </div>
                            </aura:set>
                        </aura:if>
                    </div>
                </div>
            </div>
        </lightning:card>
    </div>
    <!-- Display Lightning Data Service errors, if any -->
    <aura:if isTrue="{!not(empty(v.recordLoadError))}">
        <div class="recordError">
            {!v.recordLoadError}
        </div>
    </aura:if>

</aura:component>

JS:

({
    doInit: function (cmp, evt, hlpr) {        
        cmp.set('v.reqdField',
             cmp.getReference(
                 'v.simpleRecord.'+
                 cmp.get('v.requiredField')));
        // This is for tartgetFields, as targetFields accepts only Array of String
        var myresults =[];
        myresults.push(cmp.get('v.requiredField'));
        cmp.set('v.requiredFields',myresults);
        //alert(cmp.get('v.requiredFields'));
    }
})

DESIGN:

<design:component label="Student Photo">
    <design:attribute name="requiredField" label="Enter the Field API" description="The field should return Attachment ID" />
</design:component>

APP builder:

enter image description here

1 Answer 1

2

Basically, you can't dynamically reference things using an expression. You could, however, bind this in your controller. That would look something like this:

init: function (cmp, evt, hlpr) {
  cmp.find('reqdField')
    .set('v.value',
      cmp.getReference(
      'v.simpleRecord.'+
      cmp.get('v.requiredField')));
}

This will establish a two way binding to a dynamically selected value based on the configuration set in the attribute.

7
  • 1
    That worked @sfdcfox, good to know about getReference.
    – Ajay
    Commented Jan 25, 2018 at 19:39
  • @sfdxfox: it is become tricky, it was working with the default value provided in the component. How do I set the value to String attribute? cmp.set('v.reqdField', cmp.getReference( 'v.simpleRecord.'+ cmp.get('v.requiredField')));
    – Ajay
    Commented Jan 25, 2018 at 23:47
  • @Ajay The design component will set the attribute when specified in the app builder. If you don't specify one, it'll be set by the default value.
    – sfdcfox
    Commented Jan 26, 2018 at 0:20
  • I did specidifed the value "Student__r.Student__r.btydev__Picture_Id__c" in app builder, then i can see the console out put is: SecurePropertyReferenceValue: {!v.simpleRecord.Student__r.Student__r.btydev__Picture_Id__c} { key: {"namespace":"c"} } and console output "v.reqdField' as "null"
    – Ajay
    Commented Jan 26, 2018 at 0:33
  • @Ajay Without seeing your code, it's hard to tell, but you're probably mixing attribute names and element names.
    – sfdcfox
    Commented Jan 26, 2018 at 2:23

You must log in to answer this question.

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