Skip to main content
The 2024 Developer Survey results are live! See the results
33 votes
Accepted

Custom Metadata types not supported in Apex Classes?

getInstance method is a new addition that is supported on api version v51 and upwards. You need to update your DealTriggerHandler class api version to 51. Here is the relevant documentation
tugce's user avatar
  • 2,737
24 votes

Insert Custom Metadata for Unit Test

New in Winter 19! Instantiate Custom Metadata Types in Apex Example from the release notes... How: In this example, the first method is instantiating a custom metadata record, but no records are ...
Girbot's user avatar
  • 5,530
20 votes

Unit Test - Creating Custom Metadata

There is possible hack to have different values in tests than it is in organization. This is a good way to have your tests always stable and 100% covered even if custom metadata is changed or removed ...
Patlatus's user avatar
  • 17.3k
20 votes
Accepted

Eliminate query on Custom metadata type?

Edit: The rows themselves now count against the 50k SOQL row limit, although they do not count against the 100 SOQL query limit. To access custom metadata records without consuming SOQL rows is to use ...
sfdcfox's user avatar
  • 497k
19 votes
Accepted

How can I get the API Name of a related EntityDefinition?

Both the EntityDefinition and FieldDefinition objects have a field called QualifiedApiName that does what I am looking for: MyMetadata__mdt mapping = [ SELECT SObjectType__r.QualifiedApiName ...
Adrian Larson's user avatar
  • 151k
19 votes
Accepted

Custom Metadata vs. Custom Objects in Managed Package

Here are some "advantages" vs using a Custom Object: Can be Deployed Can use Field Definition or Entity Particle to create references to Custom Object Metadata Retrieve doesn't cost against ...
NSjonas's user avatar
  • 10.2k
19 votes
Accepted

How do I retrieve the definition of a Custom Metadata Type using CLI?

To get the Custom Metadata Type itself (including its fields), you use the CustomObject metadata type. The API name just uses __mdt instead of __c. sfdx force:source:retrieve -m CustomObject:...
Derek F's user avatar
  • 62.8k
17 votes

How do I insert records in Custom Metadata Type through Apex?

Custom Metadata Type records are considered Metadata, and as such can't be inserted or updated directly via standard DML operations. Trying to do so will result in a TypeException with a message like: ...
Daniel Ballinger's user avatar
16 votes

Custom Metadata vs. Custom Objects in Managed Package

A reason I haven't seen articulated yet applies specifically to the managed package context: Packaged, protected Custom Metadata Types are an ideal solution for secret storage in a managed package ...
David Reed's user avatar
  • 93.2k
15 votes

What is the difference between Custom Settings and Custom Metadata Types

I've done quite a bit of research on Custom Metadata Types vs Custom Settings, and I've found that in only a few cases are Custom Settings more useful. Custom Metadata types have a number of ...
SFDC Neuf's user avatar
  • 1,537
15 votes
Accepted

How to get a field's API name when querying a field of datatype, Metadata Relationship(Field Definition)

The 00N prefix is the CustomField metadata object. Unlike the API name, the field's ID will never change once created. You can query this using the Tooling API: SELECT DeveloperName FROM CustomField ...
sfdcfox's user avatar
  • 497k
14 votes

Insert Custom Metadata for Unit Test

There is possible hack to work around impossibility of insertion of custom metadata records. For this hack you just need to put query for custom metadata into separate property which should be ...
Patlatus's user avatar
  • 17.3k
14 votes
Accepted

Qualified API Name returns null in Test context for Custom Metadata

You still need to use JSON construction if you want to set parent attributes without a true query for the records. Non Functional Code EntityDefinition sourceObject = [SELECT QualifiedApiName FROM ...
Adrian Larson's user avatar
  • 151k
13 votes
Accepted

Massive Spring '17 bug: SOQL returns wrong Custom Metadata Type picklist value

You can track this bug at https://success.salesforce.com/issues_view?id=a1p3A000000eshdQAA It's scheduled to be fixed in the next patch release. We've identified the exact set of circumstances that ...
Avrom Roy-Faderman's user avatar
13 votes

Eliminate query on Custom metadata type?

Just An Update: Salesforce is coming up with new methods to retrieve custom metadata type records in a similar way custom setting records are retrieved. Yes, Without SOQL query. and It's happening in ...
Akshay Poddar's user avatar
12 votes
Accepted

Custom MetaData Types reference in Formula Field

Unfortunately, you can't reference custom metadata types in formulas at all. Their primary purpose is to provide metadata that's easy to package and transport between orgs, and primarily intended for ...
sfdcfox's user avatar
  • 497k
11 votes

Insert Custom Metadata for Unit Test

As stated above, you can't insert Custom Metadata via Apex, but you create instances of mock data via JSON deserialization. public static SObject getMetadata(SObjectType metadataSObjectType, Map<...
Scott Pelak's user avatar
  • 7,564
11 votes

How to fetch Custom Metadata Type record in apex and compare it with a string

From Spring 21 there is new way for accessing record inside Custom Metadata. Accessing specific record: ObjectName__mdt.getInstance('RecordName'); Accessing all records (and you can loop them): ...
Brane's user avatar
  • 1,052
10 votes

Unit Test - Creating Custom Metadata

Salesforce Developers put out the blog post Testing Custom Metadata Types that includes the section Testing Global Custom Metadata. ...add an extra field to your custom metadata type to indicate ...
Daniel Ballinger's user avatar
10 votes
Accepted

Deploying Metadata through Metadata.Operations.enqueueDeployment fails with a null message

There can be several reason of this deployment error. Possible reason is mandatory field is missed or the referenced metadata name is incorrect. Below is the working code to create and update Custom ...
Ashwani's user avatar
  • 22.6k
10 votes
Accepted

Can I Create Child Custom Metadata records through the Metadata API?

I actually just found the answer to this question here. The documentation states the following: When setting the value for relationship fields, use the qualified API name of the related ...
Daniel Fuller's user avatar
10 votes
Accepted

Do Custom Metadata Queries Count Against Query or Query Row Limits?

The query limit does not apply (unless you select a rich text field). However, the row limit still applies. This conclusion can easily be proven by running a simple script: List<My_Metadata__mdt>...
9 votes

Unit Test - Creating Custom Metadata

This is an old question, but one or two releases ago a new answer became available, so I thought I'd post it here. You now can instantiate custom metadata records, and set their values, the same way ...
Avrom Roy-Faderman's user avatar
9 votes
Accepted

Why are where clause comparison operators case-sensitive when using SOQL to query custom metadata?

This sounds like a bug. Please file a case for us. UPDATE: We're working on a fix.
Avrom Roy-Faderman's user avatar
9 votes
Accepted

How to delete Custom MetaData Records Via Apex?

Yes, It is possible to delete metadata records using MetadataService.deleteMetadata method. Here is a sample method call that should work: List<String> recordsToDelete = new List<String>();...
Ashish Narkhede's user avatar
9 votes

Reference Custom Metadata Type from Lightning Component

Custom Settings and Custom Metadata types can't be referenced directly in Lightning Component Markup. You have to use Apex layer for it. Src: https://help.salesforce.com/articleView?id=...
Pranay Jaiswal's user avatar
8 votes

Custom MetaData Types reference in Formula Field

You can reference CMT records in formula fields starting with the Winter '19 release. It follows the same syntax as in validation rules: $CustomMetadata.MyCustomSetting__mdt.DeveloperName....
Vladimir Gerasimov's user avatar
8 votes
Accepted

Anyway to mass delete Custom Metadata Type records?

As Custom Metadata Records are metadata you can use Force.com Migration Tool and deploy the destructive changes content of destructiveChanges.xml <?xml version="1.0" encoding="UTF-8"?> <...
Oleksandr Berehovskyi's user avatar
8 votes

Use Custom Metadata as lookup instead of hard coding

Yes, you're doing it right. Your code would look approximately like this: for(Account record: Trigger.new) { AccountGroup__mdt[] value = [select GroupType__c FROM AccountGroup__mdt WHERE ...
sfdcfox's user avatar
  • 497k
8 votes
Accepted

Remove Custom Metadata w/ Custom Metadata API

My understanding is that the Metadata.DeployContainer is the collection of components you want to deploy via Apex. When you call removeMetadata(md) or removeMetadataByFullName(fullName) you are ...
Daniel Ballinger's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible