0

Is it possible to get the value of a "lightning: outputField" to pass it to the controller? I used a lightning:recordViewForm to display record's values.

<lightning:recordViewForm recordId="{!v.recordId}" objectApiName="Contact">      
    <lightning:card title="Contact Details">          
        <p class="slds-p-horizontal_small">
            <div class="slds-grid">
                <div class="slds-col slds-size_1-of-2">                        
                    <lightning:outputField fieldName="FirstName" />    
                    <lightning:outputField fieldName="LastName" />    
                    <lightning:outputField fieldName="MobilePhone" />    
                </div>
                <div class="slds-col slds-size_1-of-2">
                    <lightning:outputField fieldName="AccountId" />    
                    <lightning:outputField fieldName="Department" />    
                    <lightning:outputField fieldName="Birthdate" />    
                </div>
            </div>
        </p>
    </lightning:card>        
</lightning:recordViewForm>

For example how can i get the value of mobilephone field?

1

No, you would need a force:recordData component instead. Since both are built on LDS (Lightning Data Service), you can get the data "for free" (e.g. no extra round-trip to the server) by using force:recordData.

  • @sfdcfox I have actually done this. – disasterlooms Apr 17 at 17:03
0

@sfdcfox I have actually done this on inputfield, and if irc, you can do on output as well. I cannot test because salesforce is down for me including my sandbox.

<lightning:inputField fieldName="VIP_Request_Type__c" required="true" onchange="{!c.validate}" value="{!v.vip}"/> 

in my controller, I called the cmp.get("v.vip") to get the value. I really dislike recorddata component and much prefer

I think it can be tricky though to make sure the component is rendered with a value before it is sent to the controller otherwise you may get a null value.

  • (a) this won't work with outputField at all. You can assign the attributes value and onchange, but they do nothing. (b) even with inputField, the user would need to modify the field's value before it will fire an onchange. As much as one might dislike force:recordData, it's the only solution outside of writing Apex code. Also, FYI, the various lightning:*form elements internally use force:recordData as well. – sfdcfox Apr 17 at 18:05
  • can't you use inputfield and then make it read only? – disasterlooms Apr 17 at 18:15
  • No. The value won't be updated until a change occurs (triggering an aura:valueChange), and making it read-only would eliminate that possibility. I do like your ideas, they're clever, but unfortunately it just doesn't work that way. I even tried using aura:handler name="change" to see if I could get a value change event on the attribute itself, still nothing. – sfdcfox Apr 17 at 18:21

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.