Play Games

Search This Blog

Wednesday, July 7, 2021

How to navigate to record view page on button click lwc - Salesforce Globe For You

 How to navigate to record view page on button click lwc  - Salesforce Globe For You 

Solution: use the lightning-navigation service.The base class needs to extend NavigationMixin and then use methods to achieve this.

Example:

navigateToRecordViewPageLWC.html

<template>  

    <lightning-card title="Navigate To Record View Page On Click">  

      <div class="slds-p-around_small">  

        <ul class="slds-m-around_medium">

            <template for:each={lstAccount.data} for:item="account">

                <li key={account.Id} data-id={account.Id} onclick={navigateToRecordViewPage} style="color:blue; 

                text-decoration:underline;cursor:pointer; ">

                    {account.Name}

                </li>

            </template>

        </ul>

      </div>  

    </lightning-card>  

</template>  

navigateToRecordViewPageLWC.js

import { LightningElement,track,wire} from 'lwc';

import { NavigationMixin } from 'lightning/navigation';

import getAccounts from '@salesforce/apex/AccountController.getAccounts';

export default class NavigateToRecordViewPageLWC extends NavigationMixin(LightningElement) {

    @track lstAccount;

    @wire(getAccounts) wiredAccounts(data,error){

        if(data){

            this.lstAccount  = data;

            console.log(JSON.stringify(this.lstAccount));

        } else {

            console.log(error);

        }

    }

    navigateToRecordViewPage(event) {

        const selectedRecordId = event.target.dataset.id;

        console.log('Event',event);

        console.log('Event',selectedRecordId);

        this[NavigationMixin.Navigate]({

            type: 'standard__recordPage',

            attributes: {

                recordId: selectedRecordId,

                actionName: 'view',

            },

        });

    }

}

navigateToRecordViewPageLWC.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>51.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output:



Monday, July 5, 2021

How to dynamically fetch recordId in LWC salesforce - Salesforce Globe For You

 How to dynamically fetch recordId in LWC salesforce  - Salesforce Globe For You 

Scenario: When I drag this LWC component on a particular salesforce record page(for example any account record),I need to get it's Id and display it in this LWC Component.

Solution:Use @api recordId in LWC Component.

Example:

displayRecordIdInLwc.html

<template>

    <lightning-card>

        <div style="padding-left: 10px;"><strong>Record Id :</strong>{recordId}</div>

    </lightning-card>

</template>

displayRecordIdInLwc.js

import { LightningElement,api } from 'lwc';

export default class DisplayRecordIdInLwc extends LightningElement {

    @api recordId;

}

displayRecordIdInLwc.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>51.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Output:



How to get parent record related field values from a record in LWC Salesforce - Salesforce Globe For You

 How to get parent record related field values from a record in LWC Salesforce  - Salesforce Globe For You 

Scenario: From the contact record,we need to fetch it's parent Account related field (Account Name,Account Rating) values in LWC

Example:

displayParentRecordFieldLWC.html

<template >

    <lightning-card title="Contact Record Data">

        <div class="slds-m-around_medium">

            <p>Contact Name: {contactName}</p>

            <p>Account Name: {AccountName}</p>

            <p>Account Rating:{AccountRating}</p>

        </div>

    </lightning-card>

</template>

displayParentRecordFieldLWC.js

import { LightningElement, wire,api,track } from 'lwc';

import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

import CONTACT_NAME_FIELD from '@salesforce/schema/Contact.Name';

import ACCOUNT_NAME_FIELD from '@salesforce/schema/Contact.Account.Name';

import ACCOUNT_RATING_FIELD from '@salesforce/schema/Contact.Account.Rating';

export default class DisplayParentRecordFieldLWC extends LightningElement {

    @api recordId;

    @track contact;

    @wire(getRecord, { recordId: '$recordId', fields: [CONTACT_NAME_FIELD, ACCOUNT_NAME_FIELD,ACCOUNT_RATING_FIELD]})

    contact;

 get contactName() {

        return getFieldValue(this.contact.data, CONTACT_NAME_FIELD);

    }

 get AccountName() {

        return getFieldValue(this.contact.data, ACCOUNT_NAME_FIELD);

    }

    get AccountRating() {

        return getFieldValue(this.contact.data, ACCOUNT_RATING_FIELD);

    }

}

displayParentRecordFieldLWC.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?>

<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">

    <apiVersion>52.0</apiVersion>

    <isExposed>true</isExposed>

    <targets>

        <target>lightning__RecordPage</target>

        <target>lightning__AppPage</target>

        <target>lightning__HomePage</target>

    </targets>

</LightningComponentBundle>

Note: Drag this component in contact page.

Output: