0

In Apex I have a map of Map<Address__c, List<Mortgage_Account__c>>(); and I am parsing this and getting it in an LWC, mortgageSummary. Here is mortgageSummary.js:

import { LightningElement, api, wire } from 'lwc';
import getAddressesAndMortgageAccounts from '@salesforce/apex/MortgageLWCHelper.getMortgageAccountsAndSecurityAddresses';

export default class MortgageSummary extends LightningElement {

    @api recordId;
    @api addressesAndMortgageAccounts = [];
    error;

    @wire(getAddressesAndMortgageAccounts, {recordId: '$recordId'})
    wiredAddressesAndMortgageAccounts({ error, data }) {
        if (data) {
            this.addressesAndMortgageAccounts = JSON.parse(data);

        } else if (error) {
            this.error = error;
            this.addressesAndMortgageAccounts = undefined;
        }
    }
}

After parsing my json it looks like below:

{
   "Address__c:{Id=a0L25000005W8rKEAS, Address_Line_1__c=Test Street, Address_Line_2__c=Test Town}":[
      {
         "attributes":{
            "type":"Mortgage_Account__c",
            "url":"/services/data/v55.0/sobjects/Mortgage_Account__c/a0M2500000AVGJeEAP"
         },
         "Id":"a0M2500000AVGJeEAP",
         "Account_Balance__c":100000,
         "Indexed_Loan_to_Value__c":100,
         "Security_Address__c":"a0L25000005W8rKEAS",
         "Security_Address__r":{
            "attributes":{
               "type":"Address__c",
               "url":"/services/data/v55.0/sobjects/Address__c/a0L25000005W8rKEAS"
            },
            "Id":"a0L25000005W8rKEAS",
            "Address_Line_1__c":"Test Street",
            "Address_Line_2__c":"Test Town"
         }
      },
      {
         "attributes":{
            "type":"Mortgage_Account__c",
            "url":"/services/data/v55.0/sobjects/Mortgage_Account__c/a0M2500000AVGJjEAP"
         },
         "Id":"a0M2500000AVGJjEAP",
         "Account_Balance__c":50000,
         "Indexed_Loan_to_Value__c":77,
         "Security_Address__c":"a0L25000005W8rKEAS",
         "Security_Address__r":{
            "attributes":{
               "type":"Address__c",
               "url":"/services/data/v55.0/sobjects/Address__c/a0L25000005W8rKEAS"
            },
            "Id":"a0L25000005W8rKEAS",
            "Address_Line_1__c":"Test Street",
            "Address_Line_2__c":"Test Town"
         }
      }
   ]
}

My question is firstly how can I access the Address__c information. I have tried something like this: data.Address__c.Id but that doesn't work.

I also need to know how to access the mortgage account information. For example, if I wanted to get Account_Balance__c from the first mortgage account I thought it would be something like: data.Address__c[0].Account_Balance__c but this doesn't work either.

As an aside is there a tool I can use to visualise how to access different elements of a JSON object?

1 Answer 1

1

To access the first element, the access would look like this:

let firstMortageAccount = this.addressesAndMortgageAccounts[
 'Address__c:{Id=a0L25000005W8rKEAS, Address_Line_1__c=Test Street, Address_Line_2__c=Test Town}'
][0].Id;

This is pretty unsustainable, so you might want to look at Object.entries:

let addressesByKey = Object.entries(JSON.parse(data));

Now, you have a much more manageable thing that looks like:

addressesByKey = [
  ['Address__c:{Id=a0L25000005W8rKEAS, Address_Line_1__c=Test Street, Address_Line_2__c=Test Town}',
  [{ Id: 'a0M2500000AVGJeEAP', ... }, { Id: 'a0M2500000AVGJjEAP', ... }]
  ],
  ...
]

Accessing the mortgage data then looks like:

addressesByKey[0][1][0].Account_Balance__c;

Where [0][1][0] represents the first key, then the value for that key, and then the first mortgage account, or generically:

addressesByKey[addressInfo][1][accountIndex];

To be honest, it would be much better to have a wrapper class in Apex to begin with, so you can return a simple list.

Also, presuming you need to parse here, it would have been easier in your Apex code to simply return the data directly:

@AuraEnabled(cacheable=true) 
public static Map<Address__c, List<Mortgage_Account__c>> 
getMortgageAccountsAndSecurityAddresses() { ...
1
  • thanks for this! I think I will go back to my Apex and try to return the data in a better way, maybe using wrapper.
    – Luke Bray
    May 20, 2022 at 15:35

You must log in to answer this question.

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