Concepts or coding lessons of Salesforce that you can implement easily

Get records from different Objects using Wire Method

How to use @Wire to get data for Batch of records ??

In LWC project, we usually have scenario where we have to retrieve different records from different object. For this we use Wire method but are you using 2 wire methods for this ?

Then you are doing multiple calls which decreases performance. 

LWC have 'getRecords' using this we can use wire adapter to get data for a batch of records at once.

As this process in Batch style, this reduces the number of network calls which indirectly increases LWC performance.

Syntax:

@wire(getRecords, { records: [ { recordIds: string[], fields: string[] } ] })


This will return 2 parameters
Results which is batch result
hasErrors which indicates if any result is failed.


Note : If you want to get data for a single record, use getRecord instead.


Here is the example you can use:

import { LightningElement, wire } from 'lwc';
import { getRecords } from 'lightning/uiRecordApi';
import CONTACT_EMAIL_FIELD from '@salesforce/schema/Contact.Email';
import USER_EMAIL_FIELD from '@salesforce/schema/User.Email';
import USER_NAME_FIELD from '@salesforce/schema/User.Name';

export default class RecordPickerDemo extends LightningElement {
    @wire(getRecords, { 
        records: [
            {
                recordIds : ['0050XXXXXX8QSMo'],
                fields: [USER_EMAIL_FIELD],
                optionalFields: [USER_NAME_FIELD],
            },
            {
                recordIds : ['0032XXXXXXnuFFf'],
                fields: [CONTACT_EMAIL_FIELD],
            },
        ],
    })
    wiredRecords;
}

Easily Iterate Map in Lightning Web Component

In LWC project, we usually stuck how to iterate Map in Lightning Web Component
Here is the example you can use to iterate Map.

1. Create Apex class StudentController.cls:

public class StudentController {
@AuraEnabled(cacheble=true)
public static Map<String, String> fetchStudentData(){
Map<String, String> mapStudentData = new Map<String, String>();
mapStudentData.put('Om','BCS');
mapStudentData.put('Sai','BTech');
mapStudentData.put('Ram','BE Computer Science');
mapStudentData.put('Radhe','BE Civil');
return mapStudentData;
}
}


2. Create LWC LWCStudentMap.html:

<template>
    <lightning-card icon-name="standard:account" variant="base" title="LWC Map Demo">
      <table class="slds-table slds-table_cell-buffer slds-table_bordered">
        <thead>
            <tr class="slds-line-height_reset">
                <th scope="col"> Name </th>
                <th scope="col"> Faculty </th>
            </tr>
        </thead>
        <tbody>
            <template for:each={mapStudentData} for:item="objMap">
                <tr class="slds-hint-parent" key={objMap.key}>
                    <th scope="col">
                        <div title={objMap.key}> {objMap.key} </div>
                    </th>
                    <th scope="col">
                        <div title={objMap.value}> {objMap.value} </div>
                    </th>
                </tr>
            </template>
        </tbody>
      </table>>
    </lightning-card>
</template>


3. Create LWC LWCStudentMap.js:

import { LightningElement, track, wire } from 'lwc';
import fetchStudentData from '@salesforce/apex/StudentController.fetchStudentData'

export default class LWCStudentMap extends LightningElement {
    @track mapStudentData = [];
    @track error;

    @wire(fetchStudentData)
    wiredData({ error, data }) {
      if (data) {
        console.log('Data', data);
        var consts = data;
        for(var key in consts)
            this.mapStudentData.push({value:consts[key], key:key});
      } else if (error) {
        console.error('Error:', error);
        this.error = error;
      }
    }
}


Output: 


Subscribe blog for More updates !!! 


Read More: WhatsApp on Salesforce Integration in 5 Steps
                    Which Salesforce API Do I Use in Integration?
                    When To Use Salesforce REST API  
                   When To Use Salesforce SOAP API 
                   When To Use Salesforce Bulk API 


Latest Salesforce Interview Questions and Answers: