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
Angel ZAngel Z 

LWC access to records' fields during iteration

Hi guys,

I need a piece of advice. I have a LWC which displays records returned from an apex method.

Here's the markup:
<template if:true={customObjectRecords.data}>
    <template for:each={customObjectRecords.data}   for:item="com">
      <div key={com.Id}>
        <p>
          <span>{com.CustomField1__c}</span>
	  <span>{com.CustomField2__c}</span>
        </p>
        <p>
          <span>{com.Name}</span>
	  <span>{com.CustomField3__c}</span>
        </p>
      </div>
    </template>
  </template>
  <template if:true={error}>
    {error}
  </template>
And here's the JS:
 
import { LightningElement, wire, track, api } from 'lwc';
import callApex from '@salesforce/apex/ApexClass.apexMethod';

export default class className extends LightningElement {
  @track customObjectRecords;

  @wire(callApex, {userContactId: 'parameter'})
    
    customObjectRecordsData(data,error) { 
        if(data){
            console.log('Data ====>', JSON.stringify(data));           
            this.customObjectRecords=data;

        } else if(error){
            console.log('error -->'+error);   
        }
    } 
}

How do I access fields of the returned customObjectRecords?

I need to do a check on a custom field for each returned record.

Thanks.

 
Best Answer chosen by Angel Z
David Zhu 🔥David Zhu 🔥
In that case, I would suggest add a new property (DISPLAY_CUSTOM_MESSAGE) in the Apex wrapper class, and put checking logic in Apex method.

All Answers

David Zhu 🔥David Zhu 🔥
If you need to check the data in apex wired call and only the record matching a condition to be displayed, you may refer the code below.

in HTML file, make the following change:

<template for:each={customObjectRecords}   for:item="com">

In js controller:

import { LightningElement, wire, track, api } from 'lwc';
import callApex from '@salesforce/apex/ApexClass.apexMethod';

export default class className extends LightningElement {
  @track customObjectRecords = [];

  @wire(callApex, {userContactId: 'parameter'})
    
    customObjectRecordsData(data,error) { 
        if(data){
            console.log('Data ====>', JSON.stringify(data));           
            
            for (let v in data.data)
            {
                 if (data.data[v].customField__c)
                 {
                      customObjectRecords.push(data.data[v]);
                 }
            }


        } else if(error){
            console.log('error -->'+error);   
        }
    } 
}
 
Angel ZAngel Z
Hi David,

Thanks for the reply.

However, this is not what I am aiming for.

After I check the data.data[v].customField__c value, I need to display a string message next to the returned result, depending on the value.
 
<template if:true={customObjectRecords.data}>
    <template for:each={customObjectRecords.data}   for:item="com">
      <div key={com.Id}>
        <p>
          <span>{com.CustomField1__c}</span>
	      <span>{DISPLAY_CUSTOM_MESSAGE}</span>
        </p>
        <p>
          <span>{com.Name}</span>
	  <span>{com.CustomField3__c}</span>
        </p>
      </div>
    </template>
  </template>
  <template if:true={error}>
    {error}
  </template>
 
import { LightningElement, wire, track, api } from 'lwc';
import callApex from '@salesforce/apex/ApexClass.apexMethod';

export default class className extends LightningElement {
  @track customObjectRecords = [];

  @wire(callApex, {userContactId: 'parameter'})
    
    customObjectRecordsData(data,error) { 
        if(data){
            console.log('Data ====>', JSON.stringify(data)); 
	        this.customObjectRecords=data;         
	        
            for(let v in data.data) {
                if(data.data[v].customField__c == 'SOMEVALUE') {
                    this.{DISPLAY_CUSTOM_MESSAGE} = 'OK';
                } else {
                    this.{DISPLAY_CUSTOM_MESSAGE} = 'NOT OK';
                }
            } 

        } else if(error){
            console.log('error -->'+error);   
        }
    } 
}

When I find a match, the custom message is the same for all records (which makes sense).

Thanks.
David Zhu 🔥David Zhu 🔥
In that case, I would suggest add a new property (DISPLAY_CUSTOM_MESSAGE) in the Apex wrapper class, and put checking logic in Apex method.
This was selected as the best answer