0

I am working on my first Lightning component and cannot figure out how to access values on the parent record when creating a child record. The component is shown on the parent record and I can get the ID of the parent record but I can't figure out how to access other values from the parent.

In the example below I am trying to put the parent record name into the Name field of the child record. I have tried multiple syntax versions and cannot get anything to work.

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <aura:attribute name="disabled" type="Boolean" default="false" />
    <aura:attribute name="saved" type="Boolean" default="false" />
    <aura:attribute name="showSpinner" type="Boolean" default="true" />
    <aura:attribute name="recordId" type="String" />
    <aura:if isTrue="{!v.showSpinner}">
        <lightning:spinner />
    </aura:if>
    <aura:if isTrue="{!!v.saved}">
        <lightning:recordEditForm
            onload="{!c.handleLoad}"
            onsubmit="{!c.handleSubmit}"
            onsuccess="{!c.handleSuccess}"
            objectApiName="Employee_Session__c"
            >
            <!-- the messages component is for error messages -->
            <lightning:messages />
            <lightning:inputField fieldName="Name" value="{!v.recordID.Name + ' MM/dd/yyyy'}"/>
            <lightning:inputField fieldName="Employee__c" value="{!v.recordId}" />

*subsequent code removed as not needed.

Populating the field Employee__C works without issue as I have the recordID but I can't get the other field to work.

  • 1
    if you are starting out with lightning, i would recommend you focus on lightning web components instead of aura. you will also notice that once you do have the related record id, you will be able to use a base component to render the related fields based on that id or use the User Interface API to fetch those values or the uiRecordApi module as a last resort. – glls Jun 24 at 15:44
1

What you can do is create an attribute and value for inputfield.

<aura:attribute name="nameUpdated" type="String" />
<lightning:inputField fieldName="Name" value="{!v.nameUpdated}"/>

then in your JS you will work

onload : function(cmp,event,handler){
    var somedate = ' whatever you want to add';
    var name = cmp.get("v.nameUpdated");
    cmp.set("v.nameUpdated",name + somedate);

    }

you will need to make sure the load happens first. Also, you may want to do something on change or have a validation rule in your JS before submitting if you don't want user to change anything.

You can't set the input field for a value unless that value is set via default or in JS. You can do this in JavaScript Controller.

edited:

  <design:component >
    <design:attribute name="title" label="title" />
    <design:attribute name="wherestmnt" label="Query Where Statement..must start with and" description="Name of the person you want to greet" />
  <design:attribute name="addfields" label="add relationship fields...must start with comma" />
<design:attribute name="orderstmnt" label="Query order statement" />
</design:component>

The design allows you to add the fields you want right on lightning record pages when you add. By having a generic set up, you can reuse this for any object. It takes some time to build, but you will probably re-use this puppy several times. Below is example of the Server Controller. In the example, this has the object I want, but it is better to replace with a variable and create a field to select the object in the design template. I don't have access to it right now, but I wanted to give you an example of how you can add parent fields and change it going forward without changing the apex code.

   public class SpaProducts {
    @AuraEnabled
    public static List<QuoteLineItem> getOppLines(String quoteid,String 
     wherestmnt,String addfields,string orderstmnt){
    system.debug('oppid is ' +quoteid);   

    DescribeSObjectResult describeResult = QuoteLineItem.getSObjectType().getDescribe();

    List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() );

    String query =        ' SELECT ' +            String.join( fieldNames, ',' ) +  addfields +   ' FROM '
        +             describeResult.getName()  +' WHERE QuoteId = : quoteid '+ wherestmnt+ orderstmnt;     
    // return generic list of sobjects or typecast to expected type

    List<SObject> records = Database.query( query );        
    System.debug( records );

    return records;
}
  • This still doesn't appear to access the "Name" value from the parent record unless I am missing something. Can you please clarify? – Matt M Jun 25 at 14:48
  • In addition, I want to access multiple values from the parent record. Is there no way to grab the parent record like you do in Apex and just reference the values within. Like "ParentObject pobj = [Select ID,Name,DOB__c ....]" and then on each of the fields I want to set I could just do "Name = pobj.Name + varDate" and "eDOB__c = pobj.DOB__C". – Matt M Jun 25 at 14:53
  • You won't get parent record values using lightning;inputfield. Some people use recorddata component to get parent records. I personally hate that solution because it means you have to have to time the synchronization of multiple elements. It can work but I find it buggy. The other way is server side controller. I have used this. What I did is create a class that uses string values to set my query. That way I can put all the fields I want right in the lightning record page, including parent recors up to 5 levels up. I can even do sub queries. I use this class on several objects. image added. – disasterlooms Jun 25 at 16:09

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.