You need to sign in to do that
Don't have an account?

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:
How do I access fields of the returned customObjectRecords?
I need to do a check on a custom field for each returned record.
Thanks.
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.
All Answers
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);
}
}
}
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.
When I find a match, the custom message is the same for all records (which makes sense).
Thanks.