The field picklist
on the CustomField object is deprecated. You will have to use the valueSet
field instead.
Here is how the updated code would look like
MetadataService.MetadataPort service = createService();
MetadataService.CustomField customField = new MetadataService.CustomField();
customField.fullName = 'Lead.picklist__c';
customField.label = 'picklist';
customField.type_x = 'Picklist';
// Define the Value Set and Value Set Definitions
MetadataService.ValueSet vs = new MetadataService.ValueSet();
MetadataService.ValueSetValuesDefinition vd = new MetadataService.ValueSetValuesDefinition();
// Define the picklist values
List<MetadataService.CustomValue> customValues = new List<MetadataService.CustomValue>();
MetadataService.CustomValue cv1 = new MetadataService.CustomValue();
cv1.label = 'One';
cv1.fullName = 'One';
cv1.isActive = true;
cv1.default_x = false;
customValues.add(cv1);
MetadataService.CustomValue cv2 = new MetadataService.CustomValue();
cv2.label = 'Two';
cv2.fullName = 'Two';
cv2.isActive = true;
cv2.default_x = true;
customValues.add(cv2);
vd.value = customValues;
vd.sorted = true;
vs.valueSetDefinition = vd;
// Use valueSet instead of Picklist
customField.valueSet = vs;
List<MetadataService.SaveResult> results =
service.createMetadata(
new MetadataService.Metadata[] { customField });
I also had to make one more change to the MetadataService.cls
as per this answer which is to replace
public virtual class CustomValue extends Metadata {
public String color;
public Boolean default_x;
public String description;
public Boolean isActive;
public String label;
}
with
public virtual class CustomValue extends Metadata {
public String type = 'CustomValue';
public String fullName;
private String[] fullName_type_info = new String[]{'fullName',SOAP_M_URI,null,'0','1','false'};
public String color;
public Boolean default_x;
public String description;
public Boolean isActive;
public String label;
private String[] color_type_info = new String[]{'color',SOAP_M_URI,null,'0','1','false'};
private String[] default_x_type_info = new String[]{'default',SOAP_M_URI,null,'1','1','false'};
private String[] description_type_info = new String[]{'description',SOAP_M_URI,null,'0','1','false'};
private String[] isActive_type_info = new String[]{'isActive',SOAP_M_URI,null,'0','1','false'};
private String[] label_type_info = new String[]{'label',SOAP_M_URI,null,'0','1','false'};
private String[] apex_schema_type_info = new String[]{SOAP_M_URI,'true','false'};
private String[] field_order_type_info = new String[]{'fullName','color','default_x','description','isActive','label'};
}