Custom metadata. The Salesforce thing 😎

records that are part of the package.

Vinod Patel
4 min readSep 2, 2020

Guys let’s start with some theory.

Custom metadata types are also like custom objects that we create to store our salesforce records but have some different usability. Let’s assume that you want some of the records as a part of the package. Your subscriber simply needs to install the package and these records are available, Or you need some records as the part of the package so that your consultant can do some configuration for your subscriber like adding some app secrets. And you don’t want your subscriber to know your app secret. Right?

For these types of requirements, we use custom metadata. We can query the custom metadata in apex and there is one more feature, and that is, we can protect our record that should not be visible to subscriber users (we can say that record level protection).

Visibility

  • PackageProtected — When in a second-generation managed package, only Apex code in the same managed package can see the type. The name of the type and the record are visible if they’re referenced in a formula.
  • Protected — When in a managed package, only Apex code in the same namespace can see the type. The name of the type and the record are visible if they’re referenced in a formula.
  • Public — Regardless of the type of package (managed or unmanaged), the following have access: Apex, formulas, Flows

Well, the theory is over let’s do some coding. đŸ‘šđŸ»â€đŸ’»

Now you are probably willing to know how you can create a custom metadata record on your org. Too early


To do so you firstly need to create a custom metadata type. Let’s follow the steps.

  1. Go to your project directory and find the object folder (force-app/main/default/) and inside this, create another folder with this convention App_configuration__mdt (do you remember we use __c for creation of custom objects? Same thing folks.)
  2. Now we also need the fields so we create the fields folder inside the App_configuration__mdt folder.
  3. Now create the App_configuration__mdt.object-meta.xml file inside the App_configuration__mdt folder (not in the fields folder.)
  4. Paste the code in the object-meta.xml
<?xml version=”1.0" encoding=”UTF-8"?>
<CustomObjectxmlns=”http://soap.sforce.com/2006/04/metadata">
<label>App Configuration</label>
<pluralLabel>App Configuration</pluralLabel>
<visibility>PackageProtected</visibility>
</CustomObject>

5. Now we need to create the fields for this custom metadata type. For that, we need to create a field meta.xml file that represents the field that is going to store our access token. So create a file with the name access_token__c.field-meta.xml inside the fields folder that we have created in the above step. And paste the code for the field configuration

<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>access_token__c</fullName>
<externalId>false</externalId>
<fieldManageability>DeveloperControlled</fieldManageability>
<label>access token</label>
<length>255</length>
<required>false</required>
<type>Text</type>
<unique>false</unique>
</CustomField>

6. Repeat step 5 for the creation of other fields like client_id or client_secret.

7. Now deploy the code to the org.

You now probably thinking that we have created a custom metadata type of packageprotected type but still, it is visible on your org. But it shouldn’t be. Right?

Don’t worry we are working in the developer org so developers can see this custom metadata type but if we create a package for it, trust me it will not be visible to the subscriber.

Now to create the record of the custom metadata type we have two ways of doing this. I will explain both.

  1. By creating the custom metadata in your package directory and deploying to the organization.
  • To do so we need to create another folder in the main directory force-app/main/default and here, Create the folder with name customMetadata
  • Now we need to create the file which is our actual custom metadata record. Create a file with exact name App_configuration.AndroidAppConfiguration.md-meta.xml
  • Now we will stores the information of the record AndroidAppConfiguration, paste the code in this file
<?xml version="1.0" encoding="UTF-8"?><CustomMetadata xmlns="http://soap.sforce.com/2006/04/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><label>Android App Configuration</label><protected>false</protected><values><field>access_token__c</field><value xsi:type="xsd:string">access-abes2323</value></values></CustomMetadata>
  • That’s it, deploy the code.
  • You can also get the record by making query [select access_token__c from App_configuration__mdt].

2. By using the apex code.

public class CreateAppConfiguration implements Metadata.DeployCallback
{
public void handleResult(Metadata.DeployResult result, Metadata.DeployCallbackContext context)
{
if (result.status == Metadata.DeployStatus.Succeeded)
{
System.debug(' success : '+ result);
}
else
{
System.debug(' fail : '+ result);
}
}
public static void createUpdateAppConfiguration(String fullName, String label, Map<String, Object> fieldWithValuesMap)
{
Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
customMetadata.fullName = fullName;
customMetadata.label = label;
for(String key : fieldWithValuesMap.keySet())
{
Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
customField.field = key + '__c';
customField.value = fieldWithValuesMap.get(key);
customMetadata.values.add(customField);
}
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
mdContainer.addMetadata(customMetadata);
CreateAppConfiguration callback = new CreateAppConfiguration();
Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);
}
}

You can call the method and can create the metadata record straight away.

CreateAppConfiguration.createUpdateAppConfiguration(‘App_configuration.AndroidAppConfiguration’, ‘Android App Configuration’, Map<String, Object>{ ‘access_token__c’ => ‘accesTokenValue’ });

That’s it you are good to go. Happy Coding!

If you are new to salesforce and looking for SFDX setup then have a look at this article. âœŒđŸ»

“Setting Up SFDX Environment. And creating your first Lighting Web Component.”

--

--