function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rita LeverettRita Leverett 

Need help to add customField to list of values in customMetadata

Hello,
Can someone help me with module Metadata API, unit Build Admin Tools for Automated Configuration Changes?

The error I'm getting is:
Line 12: Method does not exist or incorrect signature; void add(Metadata.CustomMetadataValue) from the type List

Below is my code:
public class MetadataExample {

    public void updateMetadata () {
        Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
        customMetadata.fullName = 'MyNamespace__MyMetadataTypeName.MyMetadataRecordName';
        
        Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
        customField.field = 'customField__c';
        customField.value = 'New value';
        
        List <Metadata.CustomMetadata> mdtlist = new List<Metadata.CustomMetadata>();
        mdtlist.add(customField);  // Line 12
    }

}

I feel like I'm missing something very basic but I can't find any documentation specific to what I'm trying to do and the challenge is not similar (to me) to the example. Can someone please explain where I went wrong?
Best Answer chosen by Rita Leverett
Dinesh MultaniDinesh Multani
Use below code. You can add custom field in list. 
public class MetadataExample {

    public void updateMetadata () {
        Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
        customMetadata.fullName = 'MyNamespace__MyMetadataTypeName.MyMetadataRecordName';
        
        Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
        customField.field = 'customField__c';
        customField.value = 'New value';
        
        /*Comment these lines as you can't add list
		List <Metadata.CustomMetadata> mdtlist = new List<Metadata.CustomMetadata>();
        mdtlist.add(customField);  // Line 12
		*/
		
		//Instead Use this
		customMetadata.values.add(customField);
		
    }

}

 

All Answers

Dinesh MultaniDinesh Multani
Use below code. You can add custom field in list. 
public class MetadataExample {

    public void updateMetadata () {
        Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
        customMetadata.fullName = 'MyNamespace__MyMetadataTypeName.MyMetadataRecordName';
        
        Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
        customField.field = 'customField__c';
        customField.value = 'New value';
        
        /*Comment these lines as you can't add list
		List <Metadata.CustomMetadata> mdtlist = new List<Metadata.CustomMetadata>();
        mdtlist.add(customField);  // Line 12
		*/
		
		//Instead Use this
		customMetadata.values.add(customField);
		
    }

}

 
This was selected as the best answer
Rita LeverettRita Leverett
Thank you, Dinesh. I had tried your solution earlier but I think I typed "value" instead of "values".
Kim SchaefgesKim Schaefges
I think I am close on this challenge, but I'm getting the error: 
Couldn't find use of the 'Metadata.Operations.enqueueDeployment' method to deploy the metadata, or assignment of the return value of the method to a variable called 'asyncResultId' of type 'Id'. Please double-check the instructions.

Here's my class:
public class MetadataExample {

    public class MyCallback implements Metadata.DeployCallback {
    public void handleResult(Metadata.DeployResult result,
                             Metadata.DeployCallbackContext context) {
        if (result.status == Metadata.DeployStatus.Succeeded) {
            // Deployment was successful
        } else {
            // Deployment was not successful
        }
    }
}
    
    public void updateMetadata () {
        Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
        customMetadata.fullName = 'MyNamespace__MyMetadataTypeName.MyMetadataRecordName';
        Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
        customField.field = 'customField__c';
        customField.value = 'New value';
        customMetadata.values.add(customField);
        Metadata.DeployContainer deployContainer = new Metadata.DeployContainer();
        deployContainer.addMetadata(customMetadata);
        MyCallback callback = new MyCallback();
        Metadata.Operations.enqueueDeployment(deployContainer, callback);
        Id asyncResultId = Metadata.Operations.enqueueDeployment(deployContainer, callback);}
}

Any thoughts?  Thanks!!
 
Kim SchaefgesKim Schaefges
Nevermind - I was able to solve the last line and passed! Whew!
 
Orchay NaehOrchay Naeh
solution for this chapter
 
public class MetadataExample {
      
    public void updateMetadata () {
        Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
        customMetadata.fullName = 'MyNamespace__MyMetadataTypeName.MyMetadataRecordName';
        
        Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
        customField.field = 'customField__c';
        customField.value = 'New value';
 		
		customMetadata.values.add(customField);
		
        // Create metadata container         
        Metadata.DeployContainer deployContainer = new Metadata.DeployContainer();
        
        deployContainer.addMetadata(customMetadata);

        Id asyncResultId = Metadata.Operations.enqueueDeployment(deployContainer, null);
        
	}
}
no callback is required instead use null as 2nd parameter of the function as instructed
 
KapavariVenkatramanaKapavariVenkatramana
@Orchay Naeh

Thanks for your answer....
Sunil KhuwalSunil Khuwal

This Challenge has a bit of a bad reputation, it doesn't even let user pass the challenge if you have even a single line break between some statements.

For me this line of code worked 

public class MetadataExample {
  public void updateMetadata() {
    Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
    customMetadata.fullName = 'MyNamespace__MyMetadataTypeName.MyMetadataRecordName';

    Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
    customField.field = 'customField__c';
    customField.value = 'New value';
    customMetadata.values.add(customField);

    // Add record to the container.
    Metadata.DeployContainer deployContainer = new Metadata.DeployContainer();
    deployContainer.addMetadata(customMetadata);

    // Deploy the container with the new components.
    Id asyncResultId = Metadata.Operations.enqueueDeployment(
      deployContainer,
      null
    );
  }
}

But the code on block  with a line break, challenge fails and says you are missing either customField.field or customField.value
Metadata.CustomMetadataValue customField = new Metadata.CustomMetadataValue();
    customField.field = 'customField__c';
    customField.value = 'New value';

    customMetadata.values.add(customField);
I think this challenge needs to be updated to check for content and not the exact code.