Home SalesforceApex Generic Component to update Custom Metadata

Generic Component to update Custom Metadata

by Dhanik Lal Sahni

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.

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

You may also like

Leave a Comment