+ 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....