Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    • Salesforce Spring ’25 Release: Top Flow Enhancements You Need to Know
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Friday, May 9
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»Generic Component to update Custom Metadata

    Generic Component to update Custom Metadata

    Dhanik Lal SahniBy Dhanik Lal SahniOctober 24, 2022Updated:January 12, 2025No Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Generic Component to update Custom Metadata
    Share
    Facebook Twitter LinkedIn Pinterest Email

    We create custom metadata types to make our Salesforce application configurable especially custom applications. Ideally, the admin should insert records in custom metadata but in some scenarios, other team members can also update records. This post will give step-by-step information to create a generic component to update custom metadata records. We will create a generic Lighting web component for this post.

    Steps to create Generic LWC:

    1. Create Apex class for Getting Metadata Information
    2. Create Apex class for Deploying Metadata Changes
    3. Create a generic LWC for showing and updating metadata records
    4. Create a test LWC

    Let us learn each step one by one.

    1. Create Apex class for Getting Metadata Information

    As we can create multiple metadata records for the setting application’s logic or data for the different use cases. We have to give functionality to edit selected metadata records. For the post, I am showing all metadata records in drop down, and based on selection it will show the selected record for edit.

    Generic Component to update Custom Metadata - SalesforceCodex
    Generic Component to update Custom Metadata

    We need apex code that will be used to show metadata information based on provided metadata object API. Apex returned data will be shown in lightning-combobox in LWC.

    2. Create an Apex class for Deploying Metadata Changes

    The above class was created to show metadata records, now we will create a separate class to deploy changed information.

    Custom metadata types are metadata records and they can only be updated using metadata deployment.

    Let us create a class to deploy metadata changes.

    The above class is deserializing passed generic field information and creating metadata deployment requests using Metadata.DeployContainer class. Apex class MetadataServiceCallback is a callback class that will give deployment status information.

    3. Create a generic LWC for showing and updating metadata records

    Apex classes are ready for making a generic LWC. Let us create one generic LWC which will accept metadata API and fields information. Based on those two details, component will generate UI for editing custom metadata records.

    Custom Metadata type does not support picklist but this component will support picklist using the custom lookup component. You can check the custom lookup code at github developed by Philippe Ozil.

    To add a custom lookup we have to use the below code. Lookup is added based on passed object and picklist information.

    <c-lookup key={col.name} object-name={col.object} display-name="Name" lookuplabel={col.label} query-condition="" onselected={handleCustomerChange} isselected={isCustomerSelected} selectedvalue={selectedCustomer} required selected-record-id={col.value} fieldsto-query="Id,Name" if:true={col.isPickList}></c-lookup>

    It can support all other data types like text, number, email, phone, etc. To pass field information we have to send field information in the below format. We have to pass the API name, type and label to show an object if we need a picklist. For normal data types, we need to pass object information as blank.

    _columns=[
            {
                name:'TaxRate__c',
                type:'text',
                label:'Tax Rate',
                object:''
            },
            {
                name:'TaxProductId__c',
                type:'picklist',
                label:'Tax Product',
                object:'Product2'
            }
    }

    Code for the generic component:

    We have to create two below public properties to pass metadata api and field informations to make it generic. Based on these values we will prepare UI.

    • metaApiName
    • columns

    4. Create a test LWC for using a generic component

    We can create a test LWC component to use the above-created generic component.

    To test this functionality, create one custom metadata type named Application Setting. Create below custom fields that we need to use for application configuration.

    Field API NameField LabelData Type
    TaxProductId__cTax Product IdText
    TaxRate__cTax RateNumber
    EmailTemplateName__cEmail Template NameText
    Active__cActiveCheckbox

    Create metadata records to test functionalty.

    LWC Code

    Test

    References:

    Get Started with Custom Metadata Types Unit 

    Other Related Posts:

    Get All Used Custom Metadata Detail

    Optimize Apex Code by Metadata Caching

    Find Referenced Metadata using Salesforce Dependency API

    Build Scalable Solutions with Salesforce

    change custom metadata types custom metadata types lighting lightning lightning component Lightning web component lwc metadat CRUD metadata metadata changes in apex metadata deployment metadata update update metadata value using apex
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleGenerate PDF using jsPDF in Lightning Web Component
    Next Article Optimize Salesforce Reports and Dashboard
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Basic Queueable Chaining in Salesforce Apex

    March 31, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) custom metadata types (5) design principle (9) file upload (3) flow (14) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (139) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce GraphQL API (3) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) shopify api (3) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) custom metadata types (5) design principle (9) file upload (3) flow (14) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (139) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce GraphQL API (3) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) shopify api (3) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.