Skip to main content

Hi Team,

We have two custom Metadatas with master detail relationship. One of the record in parent metadata contains 40 records in child metadata. I want to create same 40 records with different parent id. Clone functionality will work? What is the easiest way to do this?

Below code is not working

 

List<child__mdt> adjts =[select id,Type__c from child__mdt  where Parent__r.label='Test record']; (40 records from child) Parent__mdt type = [select id from Parent__mdt where label='Test rec']; (Parent record which is used to create 40 child record) List<child__mdt> icadjust = new List<child__mdt>(); for(child__mdt c:adjts){  c.Type__c= type.id; icadjust.add(c); }

icadjust.clone();

6 Antworten
  1. 15. Okt., 10:29

    Hi @Yalamanchili Naga Sajitha

    , 

    Clone() won’t work here. Custom metadata records can’t be inserted or updated with DML, so there’s nothing to clone into. You have to create them with the Metadata API via Metadata.Operations.enqueueDeployment. 

    What’s wrong with the snippet 

    • icadjust.clone() only copies the list in memory and discards the result. It doesn’t save anything.
    • Setting c.Type__c = ... on a queried record only changes the in-memory copy. CMDT records can’t be updated or inserted with DML.
    • [type.id](https://type.id/) is a paste artifact. It should be type.Id.

     

    Working approach (Anonymous Apex or a class)

    // 1. Query the source child records, including every custom field you want copied

    List<child__mdt> source = [

    SELECT DeveloperName, MasterLabel, Type__c, Other_Field__c

    FROM child__mdt

    WHERE Parent__r.Label = 'Test record'

    ];

    // 2. Get the target parent (the relationship value is based on DeveloperName, not Id)

    Parent__mdt target = [SELECT DeveloperName FROM Parent__mdt WHERE Label = 'Test rec' LIMIT 1];

    // 3. Build new CMDT records

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

    for (child__mdt c : source) {

    Metadata.CustomMetadata md = new Metadata.CustomMetadata();

    String newName = target.DeveloperName + '_' + c.DeveloperName; // DeveloperName must be unique

    md.fullName = 'child__mdt.' + newName.left(40); // keep within the name length limit

    md.label = c.MasterLabel;

    // Reparent via the relationship field

    Metadata.CustomMetadataValue parentVal = new Metadata.CustomMetadataValue();

    parentVal.field = 'Parent__c';

    parentVal.value = target.DeveloperName;

    md.values.add(parentVal);

    // Copy the other fields

    Metadata.CustomMetadataValue typeVal = new Metadata.CustomMetadataValue();

    typeVal.field = 'Type__c';

    typeVal.value = c.Type__c;

    md.values.add(typeVal);

    // ...repeat for each remaining field

    container.addMetadata(md);

    }

    // 4. Deploy (asynchronous)

    Id jobId = Metadata.Operations.enqueueDeployment(container, null);

    System.debug('Deployment job: ' + jobId);

    Things to watch 

    • The deployment is asynchronous. Check progress in Setup > Deployment Status, or pass a Metadata.DeployCallback as the second argument to log success or failure.
    • Query DeveloperName and MasterLabel, not just Id, since the deployment is keyed on fullName.
    • The format for a metadata relationship value (plain parent DeveloperName versus ParentType__mdt.DeveloperName) is something I haven’t verified against your org. If the deploy fails with a relationship error, try the qualified form.
    • This relationship field is technically a Metadata Relationship, not a true master-detail. It behaves similarly but doesn’t cascade-delete the same way.
    • Run this in a sandbox first, then deploy to production through your normal path.

    Easier no-code alternatives, if this is a one-off

     

     

    • Export the 40 child rows via Workbench or Data Loader-style tooling. You can also retrieve them with sf project retrieve start -m CustomMetadata:child.*.
    • Then copy and rename the .md-meta.xml files and update the parent reference.
    • Redeploy with sf project deploy start. This is often the fastest option for a small, one-time copy.

     I hope you find the above information helpful. If it does, please mark it as Best Answer to help others too.

0/9000