1

Can I create a Metadata Relationship field using the Metadata API?

In the snippet below, PosInfo__c is a Metadata Relationship type field. The code executes but the field is not inserted.

Creating a text or checkbox field using the same approach works fine.

Metadata.CustomMetadata Metadata =  new Metadata.CustomMetadata();
Metadata.fullName = 'FieldState';
Metadata.label = 'State';

Metadata.CustomMetadataValue CustomField = new Metadata.CustomMetadataValue();
CustomField.field = 'PosInfo__c';
CustomField.value = 'Position_info';
Metadata.values.add(CustomField);

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

Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, null);
2
  • 1
    Which is the exact type of PosInfo__c field(Field definition, entity definition, or another cmt)? Jul 20, 2020 at 12:08
  • @OlehBerehovskyi the data type is Metadata Relationship
    – Daft
    Jul 20, 2020 at 13:26

1 Answer 1

1

Metadata Relationship of type Field definition just works fine like any other fields. The value you have there does not look right.

Position_info is clearly does not look like a custom field here.

Here is a sample that worked fine for me

Metadata.CustomMetadata customMetadata  =  new Metadata.CustomMetadata();
customMetadata.fullName = 'Customer_Fields.Space';
customMetadata.label = 'Space Test';

Metadata.CustomMetadataValue CustomField = new Metadata.CustomMetadataValue();
CustomField.field = 'Sobject_Type__c';
CustomField.value = 'Space__c';
customMetadata.values.add(CustomField);

Metadata.CustomMetadataValue CustomField2 = new Metadata.CustomMetadataValue();
CustomField2.field = 'Customer_Name__c';
CustomField2.value = 'Predicted_Demand__c';
customMetadata.values.add(CustomField2);

Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
mdContainer.addMetadata(customMetadata);
Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, null);

I am using https://github.com/trailheadapps/easy-spaces-lwc for experimenting this.

2
  • I tried adding the __c, also with and without a preceding namespace. It still does not work.
    – Daft
    Jul 20, 2020 at 13:23
  • Try namespace__objectname.namespace__fieldname as the value and see if it works! Jul 20, 2020 at 15:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .