I am trying to display parent( Survey ) & child ( Survey participants ) details retrieved from the database ( Salesforce ).
I am using LWC ( Lightning web component ) to display the details.
There is a limitation in lwc that you can’t access data from a related object using ‘__r’ ( This means related object ) directly in HTML component.
Here is my data:
JavaScript
x
27
27
1
[{
2
"Survey_Participants__r": [
3
{
4
"Survey__c": "a0c55000002uRCzAAM",
5
"Contact__r": {
6
"Name": "Soumen Jana",
7
"Id": "0035500001CBDnVAAX"
8
},
9
"Id": "a0d5500000Vv6DpAAJ",
10
"Contact__c": "0035500001CBDnVAAX",
11
"Status__c": "Sent"
12
},
13
{
14
"Survey__c": "a0c55000002uRCzAAM",
15
"Contact__r": {
16
"Name": "Dhananjay Dheru",
17
"Id": "0035500001CUbS5AAL"
18
},
19
"Id": "a0d5500000Vv6DqAAJ",
20
"Contact__c": "0035500001CUbS5AAL",
21
"Status__c": "Sent"
22
}
23
],
24
"Id": "a0c55000002uRCzAAM",
25
"Batch_Id__c": "0",
26
"Status__c": "Sent"}]
27
I need to prepare it something like this :
JavaScript
1
21
21
1
[{
2
"Survey_Participants__r": [
3
{
4
"Survey__c": "a0c55000002uRCzAAM",
5
"ContactName": "Soumen Jana",
6
"Id": "a0d5500000Vv6DpAAJ",
7
"Contact__c": "0035500001CBDnVAAX",
8
"Status__c": "Sent"
9
},
10
{
11
"Survey__c": "a0c55000002uRCzAAM",
12
"ContactName": "Dhananjay Dheru",
13
"Id": "a0d5500000Vv6DqAAJ",
14
"Contact__c": "0035500001CUbS5AAL",
15
"Status__c": "Sent"
16
}
17
],
18
"Id": "a0c55000002uRCzAAM",
19
"Batch_Id__c": "0",
20
"Status__c": "Sent"}]
21
I have tried using two-approach but none of them are working as expected :
Approach 1: I am trying to use collection.map
JavaScript
1
2
1
data.map(record => Object.assign({some data manipulation here},record);
2
It’s not allowing to iterate through the nested structure.
Approach 2: nested data.foreach method.
Please let me know how I can fix the above issue.
Thank you in advance.
Advertisement
Answer
Here is a working stackblitz example