[SalesForce] How to update custom Metadata Type Relationship Field using Metadata API

I want to update custom Metadata Relationship Field using Metadata API. I am using MetadataService.CustomMetadataValue to add the field and value parameters, but not successful in updating the Relationship field. Although I am able to update other custom Metadata Type field

Below is the code I am using

MetadataService.CustomMetadataValue cmTest = new MetadataService.CustomMetadataValue(); <br/>
cmTest.field='RelationshipField__c'; <br/>
cmTest.value= 'Id of the Parent Record';

Any help on this.

Best Answer

Are you updating an existing field? Or creating a new one and adding a value?

The CustomMetadata and the CustomObject are two different Metadata types, but under CustomMetadata you will find the "values of the fields created in the CustomMetadata" and under CustomObject you will find the CustomMetadata object itself.

For instance, a CustomMetadata named "Off Limit Settings" with a number field named "AccountOffLimitMonths" will be shown as a CustomObject like (file: objects/Off_Limit_Settings__mdt.object):

<?xml version="1.0" encoding="UTF-8"?>
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
    <fields>
        <fullName>AccountOffLimitMonths__c</fullName>
        <externalId>false</externalId>
        <fieldManageability>DeveloperControlled</fieldManageability>
        <inlineHelpText>Lorem ipsum dolor</inlineHelpText>
        <label>Account Off Limit Months</label>
        <precision>18</precision>
        <required>false</required>
        <scale>0</scale>
        <type>Number</type>
        <unique>false</unique>
    </fields>
    <label>Off Limit Settings</label>
    <pluralLabel>Off Limit Settings</pluralLabel>
    <visibility>Public</visibility>
</CustomObject>

And the field will be shown under CustomMetadata as (file: customMetadata/Off_Limit_Settings.Off_Limit.md):

<?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>Off Limit</label>
    <protected>false</protected>
    <values>
        <field>AccountOffLimitMonths__c</field>
        <value xsi:type="xsd:double">12.0</value>
    </values>
</CustomMetadata>

If you want to add a new field, modify the first file and deploy it in a package. If you want to modify a field, use the second file.

Related Topic