0

I have code, that Insert and deploy custom metadata.

Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
    CustomMetadataCallback callback = new CustomMetadataCallback();
        
        Metadata.CustomMetadata customMetadata =  new Metadata.CustomMetadata();
                integer customid = Integer.valueof((Math.random() * 1000000));
                customMetadata.fullName = 'AuditTrail.id'+customid;
                customMetadata.label = 'id'+customid;
                
                
                Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
                customField.field = 'Action__c';
                customField.value = 'Field \"'+fieldLabel+'\" was changed From: '+oldfieldValue+' to '+newfieldValue;

                customMetadata.values.add(customField);
                mdContainer.addMetadata(customMetadata);
 
               Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, callback);

result: Action: Field "Description" was changed...

Everything fine till i try to insert multiple fields in custom metadata. After deploy I got only last declared field, although I thought the code should insert all fields, because i added them to customMetadata list, and than deployed it, but I was Wrong:

    Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
    customField.field = 'Action__c';
    customField.value = 'Field \"'+fieldLabel+'\" was changed From: '+oldfieldValue+' to '+newfieldValue;

    customMetadata.values.add(customField);
    mdContainer.addMetadata(customMetadata);

    customField.field = 'Made by';
    customField.value = id;

    customMetadata.values.add(customField);    
    mdContainer.addMetadata(customMetadata); 

Recult: Action: Empty Made by: some id

How can I solve this problem, and Insert a few fields at a time?

1 Answer 1

3

You declared only one instance of Metadata.CustomMetadataValue, so when you set customField.field = 'Made by'; you overwrote the previous value, no matter you already put it in the list: objects are passed by reference. Therefore you also added the same object twice in the customMetadata.values list.
If you want to reuse the same variable, you simply need a new instance:

Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
customField.field = 'Action__c';
customField.value = 'Field \"'+fieldLabel+'\" was changed From: '+oldfieldValue+' to '+newfieldValue;

customMetadata.values.add(customField);

customField = new Metadata.CustomMetadataValue(); // create a new instance
customField.field = 'Made by';
customField.value = id;

customMetadata.values.add(customField); // now there are two different objects in the list
mdContainer.addMetadata(customMetadata); // you need to add it only once

Please remember that adding an object to a list/set/map means adding its reference, not copying the whole object.

You must log in to answer this question.

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