ABSYZ ABSYZ

  • Home

    Home

  • About us

    Who We Are

  • Our Expertise

    What we Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Increase your Productivity with the Apex Metadata API

Home / Article & Blogs / Salesforce / Integration / Increase your Productivity with the Apex Metadata API
By Team ABSYZ inIntegration, Metadata API
Salesforce

The force.com Platform provides us with features which are out of the box and helps increase productivity! Such is the “Metadata API”.

Here, we will see how we can leverage the Metadata API and avoid doing tasks manually thus ultimately saving a lot of time!

What is Metadata API?

Launched back in the Summer’17 release the Apex Metadata API lets you make metadata changes directly from apex.

We can use the handy features of apex in order to build custom setup UI scripts and perform automation actions behind the script.

Let’s consider an example and see how it can help :

Suppose you are an ISV and have created an app involving automation’s and many other features which are not generally available.You have packaged all your changes and is ready to be installed across org’s.

Now, the admin team comes into picture for rolling out the changes. The admin team has to perform a lot of post installation steps manually using the setup UI over and over.

This is where the metadata API comes in, you can create custom setup UI scripts to guide the admins through the process, and perform back end change actions using metadata api which updates the metadata while they go through the process over clicks and that’s it the changes will be available.

Thus, avoiding repetitive steps.

Let’s look at few use cases where we can use the Metadata API:

  1. Using the Metadata API we can build tools which can automate configuration changes.
  2. We can update our metadata changes in multiple org’s by deploying it across multiple org’s from apex using the Metadata API Deploycallback class which uses a deployment container and executes based on actions specified.
  3. We can control multiple org metadata in a single org. And can perform CRUD operations.

There are lots of use cases around, let’s implement few and see how actually the Metadata API can help us increase productivity!

In our example here we will be retrieving the metadata from the contact object. We will retrieve a section named ‘Additional Information’ from the page layout of contact, add a new field into that section all from apex!!

Sounds Interesting ain’t that? Let’s check it out…

[sourcecode language=”Java”]

public class UpdateContactPageLayout {
public Metadata.layout addLayoutItem(){

//Retreive metadata from the contact page layout using Metadata.Operations.retrieve method
List Metadata.Metadata layoutsList =
Metadata.Operations.retrieve(Metadata.MetadataType.Layout,
new List String{‘Contact-Contact Layout’});

//Getting Layout from the contact object
Metadata.Layout layoutMetadata = (Metadata.Layout) layoutsList.get(0);
Metadata.LayoutSection contactLayoutSection = null;
List Metadata.LayoutSection layoutSections = layoutMetadata.layoutSections;

for (Metadata.LayoutSection section : layoutSections) {
/** Assigning section value to contactLayoutSection Variable if section label satisfies condition **/

if (section.label == ‘Additional Information’) {
contactLayoutSection = section;
break;
}

}

List Metadata.LayoutColumn contactColumns = contactLayoutSection.layoutColumns;
List Metadata.LayoutItem contactLayoutItems = contactColumns.get(0).layoutItems;

Metadata.LayoutItem newField = new Metadata.LayoutItem();
newField.behavior = Metadata.UiBehavior.Edit;
newField.field = ‘AMAPI__Apex_MD_API_Twitter_name__c’;
contactLayoutItems.add(newField);

system.debug(‘newfield Inserted’);
system.debug(‘contactLayoutItems’ + contactLayoutItems);
system.debug(newField);

return layoutMetadata;
}
}

[/sourcecode]

 

Let’s check out the logs and see whether a new field was created or not!

If you are trying this out instead of “amapi” in the newField.field variable specify your org’s namespace followed by the field you want to create.

E.g: ‘yourOrgNameSpace_Field_Name__c’

Screenshot (73)

We can see that the field got created, but in order to finally add the field in your layout we need to deploy the Metadata changes using Deploy container. This will finally add your metadata to the page layout.

Refer here for Deployment Container Class.

Now, let’s quickly look at another example of creating a record in a Custom Metadata Type.

In our case we have already created a Custom Metadata type called “Vat Rate”.

And we will be creating a new record for the custom Metadata Type using Custom Metadata API from apex.

If you are a newbie and have no idea how to create a custom metadata type refer this “Create Custom MetadataType“!

Let’s look at the code:

[sourcecode language=”Java”]

public class MetadataExample {
public void updateMetadata(){

//Retreive custom metadata type details from org
Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();

//insert the fullName and Label value for the record you want to create

customMetadata.fullName = ‘VAT_Rate.NewRecordFromMetadataAPI’;
customMetadata.label = ‘NewRecordFromMetadataAPI’;

//We will populate the field called Rate in the Custom Metadata type with some value
Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
customField.field = ‘Rate__c’;
customField.value = 10;

/**Adding record values to the customMetadata type ***/
customMetadata.values.add(customField);

/** Deployment Container to deploy the metadata changes **/

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

/** This will invoke the deployment Process of the changes **/
system.debug(‘customMetadata’+customMetadata);
system.debug(‘Metadata deployed’);

Id asyncResultId = Metadata.Operations.enqueueDeployment(deployContainer, null);
}
}

[/sourcecode]

Also, check the logs just to be sure there are no errors! 😀

Screenshot (74)

You can track your metadata change deployment in the Deployment Status.

Navigate to Setup –>In Quick Find type “Deployment Status” and click on the record initiated with proper timestamp details.

Screenshot (77)

Once the changes are deployed, we will navigate to the Custom Metadata Type and check whether a record has been created or not.

Let’s navigate to the Vat Rate Custom metadata type and see.

Screenshot (78)

 

Now let’s navigate to the record detail page and check whether the value of “rate” specified in code is populated or not.

Screenshot (79)

That’s it! We have created a metadata record type using the Metadata API from Apex and not manually!!

Advantages of using the Metadata API:

  1. You can deploy your metadata changes using deployable packages across multiple org’s without needing to create change sets and doing tasks manually.
  2. You can write custom scripts to perform backend actions for automating metadata changes across the org. thus eliminating a major heck of manually work for the SF admins out there!! Interesting? Ain’t that.

You can also create CustomMetadata types using the MetadataService class library.

That shall be all for now!

We will continue to dive deep on this in the next follow up blog and also check how to test your MetadataAPI and discuss possibilities of designing a solution for Bulk Creation of Custom Metadata Types and automate Metadata Changes.

Thank you for reading!!

Don’t be shy to try it out, and keep on blazing your trails!! 🙂 

 

 

 

 

 

MetaDataSalesforceSFDC
189
Like this post
127 Posts
Team ABSYZ

Search Posts

Archives

Categories

Recent posts

BioAsia 2023 in Hyderabad: An Annual International Event

BioAsia 2023 in Hyderabad: An Annual International Event

The Role Of Marketing in Small & Medium Enterprises

The Role Of Marketing in Small & Medium Enterprises

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Salesforce For Retail: How Salesforce CRM Can Help Retailers

What is ChatGPT & How Does It Work?

What is ChatGPT & How Does It Work?

What Is Graphic Design? (Executive Summary 2023)

What Is Graphic Design? (Executive Summary 2023)

  • Previous PostCreate ML - MachineLearning in swift
  • Next PostSummer'20 Release Treasure Hunt: Top Features

Related Posts

PLATFORM EVENTS IN SALESFORCE
Integration Salesforce

PLATFORM EVENTS IN SALESFORCE

Integrate SharePoint with Salesforce using Microsoft Graph API
Apex Integration Salesforce

Integrate SharePoint with Salesforce using Microsoft Graph API

Create/Update Salesforce Picklist definitions using metadata API
Integration Metadata API Salesforce

Create/Update Salesforce Picklist definitions using metadata API

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo

INDIA | USA | UAE

  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

Copyright ©2022 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT