Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fetch/customize the custom metadata and display the list of metadata in lightning component using Apex class in Salesforce Lightning Web Component — LWC #45

Open
vijayk3327 opened this issue Jul 14, 2023 · 0 comments
Assignees
Labels
documentation Improvements or additions to documentation question Further information is requested

Comments

@vijayk3327
Copy link
Owner

vijayk3327 commented Jul 14, 2023

In this post we are going to learn about How to How to fetch/customize the custom metadata and display the list of metadata in lightning component using Apex class in Salesforce Lightning Web Component — LWC .

→ Get source code live demo link:-

Step 1:- Create Lightning Web Component HTML ➡ lwcFetchCustomMetaData.html

`


How to Fetch Custom Meta Data in Lightning Web Component (LWC)




</lightning-card>

`

Step 2:- Create Lightning Web Component Javascript ➡ lwcFetchCustomMetaData.js

`import { LightningElement, wire } from 'lwc';
import fetchMetaListLwc from '@salesforce/apex/lwcCustomMetaDataCtrl.fetchMetaListLwc';
const COLUMNS = [
{ label: 'Label', fieldName: 'MasterLabel' },
{ label: 'Student Name', fieldName: 'Student_Name__c' },
{ label: 'Phone', fieldName: 'Phone__c' },
{ label: 'Email', fieldName: 'Email__c' },
{ label: 'Country', fieldName: 'Country__c' },
{ label: 'City', fieldName: 'City__c' },
];

export default class LwcFetchCustomMetaData extends LightningElement {
records;
wiredRecords;
error;
columns = COLUMNS;
draftValues = [];

@wire( fetchMetaListLwc )  
wiredRecs( value ) {

    this.wiredRecords = value;
    const { data, error } = value;

    if ( data ) {
                    
        this.records = data;
        this.error = undefined;

    } else if ( error ) {

        this.error = error;
        this.records = undefined;

    }

} 

}`

Step 3:- Create Lightning Web Component Meta XML ➡ lwcFetchCustomMetaData.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>45.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__AppPage</target> <target>lightning__RecordPage</target> <target>lightning__HomePage</target> </targets> </LightningComponentBundle>

Step 4:- Create Apex Class : lwcCustomMetaDataCtrl.cls

`public with sharing class lwcCustomMetaDataCtrl {

@AuraEnabled( cacheable=true )  
public static List < Student_Basic_Detail__mdt > fetchMetaListLwc() {
    
    List < Student_Basic_Detail__mdt > fetchMeta = [ SELECT Id, MasterLabel, City__c, Country__c, Email__c, Phone__c, Student_Name__c FROM Student_Basic_Detail__mdt ];
    return fetchMeta;

}

}`

→ Get source code live demo link:-

@vijayk3327 vijayk3327 added documentation Improvements or additions to documentation question Further information is requested labels Jul 14, 2023
@vijayk3327 vijayk3327 self-assigned this Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation question Further information is requested
Projects
Status: No status
Development

No branches or pull requests

1 participant