0

I am using the force:recordData and trying to get the field values from the recordData in JS.

Component:

<aura:attribute name="recordId" type="String" />
    <aura:attribute name="record" type="Object" />
    <aura:attribute name="simpleRecord" type="Object" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="recordSaveError" type="String"/>
    <force:recordData aura:id="internshipOriginalRecord"
                      recordId="{!v.recordId}"
                      targetRecord="{!v.record}"
                      targetFields="{!v.simpleRecord}"
                      fields="Id,
                              Name,
                              Track__c,
                              Cohort_Sem__c,
                              Job_Category__c,
                              InternshipWorkSite__c,
                              Site_Location__c,
                              Start_Date__c,
                              Employer__c,
                              Opportunity__c,  
                              Opportunity__r.Name,
                              Employer__r.Name,
                              InternshipWorkSite__r.Name,
                              Site_Location__r.Name,
                              Employer__r.Name"
                      />

JS:

var int = component.get("v.record"), param;
            param = { sobjectType: 'Apprenticeship__c', Employer__c: int.fields.Employer__c.value, Start_Date__c: int.fields.Start_Date__c.value, Site_Location__c: int.fields.Site_Location__c.value, InternshipWorkSite__c: int.fields.InternshipWorkSite__c.value, Track__c: int.fields.Track__c.value, Job_Category__c: int.fields.Job_Category__c.value, Cohort_Sem__c: int.fields.Cohort_Sem__c.value, Opportunity__c: int.fields.Opportunity__c };       

            component.set("v.simpleNewinternship.Track__c", param.Track__c);
            component.set("v.simpleNewinternship.Job_Category__c", param.Job_Category__c);
            component.set("v.simpleNewinternship.InternshipWorkSite__c", param.InternshipWorkSite__c);
            component.set("v.simpleNewinternship.Site_Location__c", param.Site_Location__c);
            component.set("v.simpleNewinternship.Start_Date__c", param.Start_Date__c);
            component.set("v.simpleNewinternship.Employer__c", param.Employer__c);
            component.set("v.simpleNewinternship.Cohort_Sem__c", param.Cohort_Sem__c);
            component.set("v.simpleNewinternship.Opportunity__c", param.Opportunity__c);
            console.log('Site: '+param.Site_Location__c);
            console.log('IWS: '+param.InternshipWorkSite__c);
            console.log('Account: '+param.Employer__c);
            console.log('Opportunity: '+param.Opportunity__c);

Log:

Site: a0N380000077iPlEAI
IWS: a4i38000001F9bAAAS Account: 0015000000lFRQvAAO
Opportunity: [object Object]
Problem saving internship, error: [{"fieldErrors":{"Opportunity__c":[{"message":"Invalid data type.","fieldApiName":"Opportunity__c","fieldLabel":"Opportunity"}]},"pageErrors":[{"message":"The Record provided contains field(s) with invalid data."}]}]

Issue: Since I am getting Opportunity value as object instead of Id, I am not able to save the record as Opportunity is required field.

If you notice, Site, IWS and Account field are also relationship fields and I am getting Id in that fields, where as not for Opportunity.

What could be the reason?

2 Answers 2

4

targetRecord has a different "shape" than targetFields, so you'd end up doing something like:

console.log(component.get("v.record").fields.Opportunity__r.fields.Name.value);

For this reason, it's usually better to use targetFields, which is much easier to work with.

3
  • do you have any Link for this behaviour? Cant find related to this in Lightning Developer Guide. Mar 15, 2018 at 19:25
  • 2
    @PranayJaiswal It was in the docs, but is mysteriously missing since the last update...
    – sfdcfox
    Mar 15, 2018 at 19:39
  • Make sense, that is why I did.
    – Ajay
    Mar 15, 2018 at 21:06
0

I used targetFields instead of targetRecord. I am getting the value, hoever not sure why I am not getting the value in targetRecord.

0

You must log in to answer this question.

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