41 votes
Accepted

Insert Custom Metadata for Unit Test

At time of writing, it is not possible to insert test custom metadata, unless you use the Metadata API which would also present a challenge in a test scenario. That said, you should not need to. ...
Phil Hawthorn's user avatar
32 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,655
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,445
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
  • 16.8k
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
  • 491k
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
  • 150k
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
  • 61.6k
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
  • 92.8k
15 votes
Accepted

Mass Insert Custom Metadata

The Data Loader, as far as I can tell, does not support Custom Metadata (and we've been given no indication if this will be available in the future). In fact, custom metadata isn't "real" ...
sfdcfox's user avatar
  • 491k
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,507
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
  • 491k
15 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
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
  • 16.8k
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
  • 150k
13 votes

Unit Test - Creating Custom Metadata

1 - Is their anyway we could create Custom Metadata in Test Classes? I tried to create on my Test Class but I got an error saying the fields of the metadata could not be writable. No, you can't ...
sfdcfox's user avatar
  • 491k
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
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,514
11 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
  • 491k
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
9 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,012
8 votes

Insert Custom Metadata for Unit Test

Take a look at the Custom Metadata Types Implementation Guide on page 14. Use SOQL to access your custom metadata types and to retrieve the API names of the records on those types. DML operations ...
Adrian Larson's user avatar
  • 150k

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