Skip to content

Ashwani Soni | Certified | Salesforce | Partner | Senior Team Lead | Consultant | Developer | Mobile | Integration | Contributor | SFSE

Custom Metadata

Create or Update Custom Metadata Type via Apex code – Salesforce Summer 17

Posted on May 2, 2017May 2, 2017 by Ashwani

Salesforce Blog Template.png

Salesforce Summer 17 came with a most wanted feature for Custom Metadata Types which is managing it via Apex code. Now custom metadata records can be create and updated via Apex code which means no changes set or force.com migration tool is required from Summer 17.

There is a Namespace called Metadata introduced in Summer ’17 release to manage Custom Metadata.

Below is the sample code to create a Custom metadata type:-

First define a Metadata in org the same as previous releases. Here Response_Type__mdt is created.

Add new field Status_Code__c.

Here our purpose is to configure status codes by Custom Metadata proper message can be displayed.

New Metadata would look like as below:-

Screenshot 2017-04-28 23.00.03
Custom Metadata

Few things to consider:-

  • Custom Metadata create/update is asynchronous
  • It actually does a deployment in backend instead of DML
  • Doesn’t count against governor limit

Because the Custom Metadata update happens asynchronous it requires a way to get the status of deployment. So, it requires callback class. Lets define a callback class as:-

public class CustomMetadataCallback implements Metadata.DeployCallback {
    public void handleResult(Metadata.DeployResult result,
                             Metadata.DeployCallbackContext context) {
        if (result.status == Metadata.DeployStatus.Succeeded) {
            System.debug('success: '+ result);
        } else {
            // Deployment was not successful
            System.debug('fail: '+ result);
        }
    }
}

A callback class implement Metadata.DeployCallback interface.

Below is the way to create a custom Metadata:-

    // Set up custom metadata to be created in the subscriber org.
    Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
    customMetadata.fullName = 'Response_Type.Not_Found_Code';
    customMetadata.label = 'Not_Found_Code';

    Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
    customField.field = 'Status_Code__c';
    customField.value = '404';

    customMetadata.values.add(customField);

    Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
    mdContainer.addMetadata(customMetadata);

    // Setup deploy callback, MyDeployCallback implements
    // the Metadata.DeployCallback interface (code for
    // this class not shown in this example)
    CustomMetadataCallback callback = new CustomMetadataCallback();

    // Enqueue custom metadata deployment
    // jobId is the deployment ID
    Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);

By executing the above code we can create a Custom Metadata record in Response_Type__mdt custom Metadata.

All failures and success can be traced in callback class CustomMetadataCallback‘s method handleResult.

In addition deployment result can be checked in Setup -> Deploy | Deployment Status as shown below:

Screenshot 2017-04-29 13.26.09.png

This is how we an create Custom Metadata record. It has been the most demanded feature since Custom Metadata introduced. Hope that will help a lot in Custom Metadata based implementations.

Useful links:-

Salesforce Summer ’17 release notes

Metadata Namespace reference

Stackexchange question on Custom metadata creation

Advertisement
Posted in ApexTagged Custom Metadata, Deployment, Metadata, Salesforce, VisualforceLeave a comment

Social

  • View ashwaniarea’s profile on Facebook
  • View ashwanisoni_’s profile on Twitter
  • View ashwaniarea’s profile on Instagram
  • View ashwaniarea’s profile on LinkedIn
  • View codemafiaa’s profile on GitHub
  • View ashwanisoniatgoogle’s profile on Google+

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 129 other subscribers

Recent Posts:

Salesforce Community and Site.com URL redirect management: An Easy Way

Salesforce1 Lightning Components: Getting started with Salesforce1 lightning components

Salesforce1 : Share/Upload photo directly to Chatter from mobile camera using Salesforce1 App

Show Custom Visualforce page in Salesforce1. Setup publisher action to allow user to check-in using Salesforce1 app

Salesforce1 look and feel. Customise Visualforce Pages for Salesforce1

Flickr Photos

Winterfall is hereMy #mac Simply.. It looks #beautiful, #powerful. The best machine for a #Salesforce #Dev #MacBook #apple #linuxfan #hackerstayawayThe back stage silence! #Salesforce #meetup 2 minutes before #rock as #rockstar #lightning #naptime #fancrowd #goingtorockthis #staytunedformore #calmbeforethestormThe #macroworld #macro flower photography #beautiful #flower20150314_204739
More Photos

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 129 other subscribers
  • View ashwaniarea’s profile on Facebook
  • View ashwanisoni_’s profile on Twitter
  • View ashwaniarea’s profile on Instagram
  • View ashwaniarea’s profile on LinkedIn
Follow Ashwani Soni | Certified | Salesforce | Partner | Senior Team Lead | Consultant | Developer | Mobile | Integration | Contributor | SFSE on WordPress.com
Blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • Ashwani Soni | Certified | Salesforce | Partner | Senior Team Lead | Consultant | Developer | Mobile | Integration | Contributor | SFSE
    • Already have a WordPress.com account? Log in now.
    • Ashwani Soni | Certified | Salesforce | Partner | Senior Team Lead | Consultant | Developer | Mobile | Integration | Contributor | SFSE
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...