function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Agnibha Chakrabarti 10Agnibha Chakrabarti 10 

use of targetRecord and targetFields in recordData in Lightning data service

Im learning the usefulness of LDS.but having a problem to understand the work of  targetRecord in recordData. can we use targetRecord attribute as {!V.targetRecord} ?
I read the documentations, but still not clear with it
Best Answer chosen by Agnibha Chakrabarti 10
Khan AnasKhan Anas (Salesforce Developers) 
Hi Agnibha,

Greetings to you!

targetRecord is the provided record. This attribute will contain only the fields relevant to the requested layoutType and/or fields atributes.
targetFields is a simplified view of the fields in targetRecord, to reference record fields in component markup.

targetFields is the corresponding attribute to simpleRecord. targetFields is easier to use compared to targetRecord. You do not need to use both, just stick with one.

Please refer to the below links which might help you further.

https://salesforce.stackexchange.com/questions/203832/confused-on-forcerecorddata

https://salesforce.stackexchange.com/questions/200430/attributes-and-field-clarification-from-lightning-data-service-trailhead-module

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Agnibha,

Greetings to you!

targetRecord is the provided record. This attribute will contain only the fields relevant to the requested layoutType and/or fields atributes.
targetFields is a simplified view of the fields in targetRecord, to reference record fields in component markup.

targetFields is the corresponding attribute to simpleRecord. targetFields is easier to use compared to targetRecord. You do not need to use both, just stick with one.

Please refer to the below links which might help you further.

https://salesforce.stackexchange.com/questions/203832/confused-on-forcerecorddata

https://salesforce.stackexchange.com/questions/200430/attributes-and-field-clarification-from-lightning-data-service-trailhead-module

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi agnibha,
According to documentation targetFields contains  simplified view of the fields in targetRecord, to reference record fields in component markup.
targetRecord  This attribute will contain only the fields relevant to the requested layoutType and/or fields atributes.
From release notes
force:recordData includes a new attribute called targetFields, which is populated with a simplified view of the record’s fields. targetFields is automatically updated whenever Lightning Data Service detects a record change. v.targetFields.Name is equivalent to v.targetRecord.fields.Name.value. A simple way to update force:recordPreview usage to force:recordData is to change references from targetRecord to targetFields.

Both has same data but only difference is syntax for reference
With targetFields we can simply reference field like this
{!v. targetFields.Name}

Using targetRecord we have to reference field in this way
{!v.targetRecord.fields.Name.value}

Please refer below links which might help you further
https://releasenotes.docs.salesforce.com/en-us/summer17/release-notes/rn_lightning_data_service.htm


Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards