I'm trying to upload a file to a related record using just the <lightning:input> component.

The <aura: component> includes the force:hasRecordId and by using {!v.recordId} I am able to target the parent id easily. However, I have a hard time targeting the id of the related record..

I've used <force:recordData> before to get fields from the related record but uploading files seem different.

I'm trying to avoid using <lightning:fileUpload> but if I do I've gone as far as this:

<aura:component implements="forceCommunity:availableForAllPageTypes,lightning:isUrlAddressable,force:hasRecordId" access="global" >

<lightning:fileUpload label="File Upload"
                      accept=".pdf, .png, .jpg"

However I'm only getting the recordId from the parent record. Is there any way I could target the child record?

Something like {!v.recordId.Related_SubRecord__c} doesn't seem to work. Please help!

  • So are you saying that you have a component which displays the parent and the child records on the same component, and that you want to upload a file from the component and that it gets associated to one of the child records? – Jayant Das Jan 23 '19 at 16:08
  • @JayantDas Yes that's exactly it! I could always manually copy/paste the child record id but I want to make it dynamic. – CJK Jan 23 '19 at 18:35
  • How will you know to which child record the file needs to be associated to? At any given point of time, you will have 1parent:Nchild records. – Jayant Das Jan 23 '19 at 18:36
  • @JayantDas The child record's API is Related_SubRecord__c with the type SubRecord__c. I was wondering if there's way to target that one specific child record. – CJK Jan 23 '19 at 18:50
  • As long as you can get that field which contains the target child record's id, you can utilize lightning:fileUpload here. Are you having trouble getting that field in your component? Posting some code here by editing your question will be helpful for anyone to be able to provide some direction. – Jayant Das Jan 23 '19 at 19:04

Rolling up information from comments:

  1. You have a custom field on a Parent Object to determine a Child Record Related_SubRecord__c
  2. You have a custom lightning component where you are displaying the Parent and the Child records, and you want to upload a file to the Child record as determined by the value in Related_SubRecord__c
  3. You need to fetch the child record's field to be able to associate the uploaded file to that record

You are on the right track for this. You can use lightning:fileUpload here to associate the uploaded file to the child record as long as you are able to get the field accessible on your component.

In your example, you can access the Related_SubRecord__c using force:recordData as below. Declare an attribute simpleRecord and then utilize that in the targetFields attribute in force:recordData. This will give you the fields from the record, and that you can access it in your component.

<aura:attribute name="simpleRecord" type="Object" />

<!-- removed other attributes for brevity -->
<force:recordData aura:id="myRecord"
                  targetFields ="{!v.simpleRecord}"
                  .... />

<!-- utilize Related_SubRecord__c's value as below -->
<lightning:fileUpload label="File Upload"
                      accept=".pdf, .png, .jpg"
  • Amazing! This worked! I did have to add fields="Related_SubRecord__c" to the <force:recordData> but other than that it works perfectly. Thank you so much!! – CJK Jan 23 '19 at 20:18
  • Yes, actually you can use fields attribute too. – Jayant Das Jan 23 '19 at 20:19

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.