How do I use custom metadata in Apex class?
How to Create/Update Custom Metadata Using Apex?
- Step 1 – Create Custom Metadata.
- Step 2 – Create a class and implement Metadata.DeployCallback interface.
- Step 3 – implement handleResult method in the class.
- Step 4 – Prepare the Custom Metadata.
- Step 5 – Add field values to the Metadata Record.
How do I add custom metadata to Apex?
Insert Custom metadata record in Apex Assign a customerId and provide 5% discount on their first purchase. Similar to update, In Insert also we need to create an instance of custom metadata and map fields accordingly. Only difference is we should set the DeveloperName and Label since we are creating a new record here.
Can we update custom metadata using apex?
You can use the below-provided Apex Class to perform the insertion and update of custom metadata records. *@Desc: A class that can be used to insert and update multiple custom metadata records in a single transaction. *@desc: A method to insert multiple records for custom metadata object in a single go.
What is the use of custom metadata?
Custom metadata is customizable, deployable, packageable, and upgradeable application metadata. First, you create a custom metadata type, which defines the form of the application metadata. Then you build reusable functionality that determines the behavior based on metadata of that type.
How to create custom metadata type in apex?
We can Create or Update Custom Metadata records using apex Metadata deployment. To do the Metadata deployment using apex, it requires a callback class with Metadata.DeployCallback interface. Here I have created a Custom Metadata Type (Tax Setting), to save Tax related information for an Application.
Can you create custom metadata in DML statement?
We cannot Create or Update Custom Metadata records using DML statement. We can Create or Update Custom Metadata records using apex Metadata deployment. To do the Metadata deployment using apex, it requires a callback class with Metadata.DeployCallback interface.
How do I create custom metadata in deploycallback?
You need to follow the below simple steps. Navigate to Setup -> Develop -> Custom Metadata Types -> New Custom Metadata Types -> Create the Custom Metadata. I have created label as Profile Setting. Refer below Screenshot After you have implemented the DeployCallback interface you need to provide the implementation of handleResult method.
How to declare variables that refer to custom metadata types?
Declaring variables that refer to custom metadata types is similar to what you already do when working in Apex. For example, declare an Apex variable of a custom metadata type like this. Now let’s get the values of a custom field from all the custom metadata records of a certain type.