1

I am using Metadata WSDL along with Partner WSDl to work on metadata API to create fields.

I am able to create fields of type text, checkbox, etc, but I need to create a picklist field and add picklist values to it.

Below is the source code i have written. I am not sure where to add the picklist values.

CustomField customField = new CustomField();
customField.setFullName("Account.customPickField__c");
customField.setLabel("customPickField");
customField.setType(FieldType.Picklist);

SaveResult[] results = metadataConnection.createMetadata(new Metadata[] {customField});

I have seen forums where people says to use

customField.setPicklist();

But this method is not available .

I am using a java application, to connection to a salesforce org with version 39.0.

2 Answers 2

2

If you check the metadata XML, you will find that after Version 37 the picklist values are not stored as picklistValue inside picklist anymore. They are stored as value inside valueSetDefinition wrapped in valueSet. Please find the difference below.

Version <= 37

<fields>
    <fullName>Country__c</fullName>
    <externalId>false</externalId>
    <label>Country</label>
    <picklist>
        <picklistValues>
            <fullName>USA</fullName>
            <default>false</default>
        </picklistValues>
        <picklistValues>
            <fullName>INDIA</fullName>
            <default>false</default>
        </picklistValues>
        <picklistValues>
            <fullName>Canada</fullName>
            <default>false</default>
        </picklistValues>
        <sorted>false</sorted>
    </picklist>

Verson >= 38

<fields>
    <fullName>Country__c</fullName>
    <externalId>false</externalId>
    <label>Country</label>
    <required>false</required>
    <trackTrending>false</trackTrending>
    <type>Picklist</type>
    <valueSet>
        <valueSetDefinition>
            <sorted>false</sorted>
            <value>
                <fullName>USA</fullName>
                <default>false</default>
            </value>
            <value>
                <fullName>INDIA</fullName>
                <default>false</default>
            </value>
            <value>
                <fullName>Canada</fullName>
                <default>false</default>
            </value>
        </valueSetDefinition>
    </valueSet>
</fields>

So you have to create same structure in your Java Code to create the Picklist with picklist values. Your java code should look like this.

CustomField field = new CustomField();
field.setType(FieldType.Picklist);
ValueSet vs = new ValueSet();
ValueSetValuesDefinition vd = new ValueSetValuesDefinition();
CustomValue[] cva = new CustomValue[2]; // The picklist contains two values
CustomValue cv1 = new CustomValue();
cv1.setFullName("Pick1"); // First picklist value
cv1.setDefault(false);
cva[0] = cv1;
CustomValue cv2 = new CustomValue();
cv2.setFullName("Pick2"); // Second picklist value
cv2.setDefault(false);
cva[1] = cv2;
vd.setValue(cva);
vs.setValueSetDefinition(vd);
field.setDescription("Created by Metadata API");
field.setLabel("Metadata API");
field.setFullName("Book__c.Metadata_API__c");
field.setValueSet(vs);
1
  • Related question; if you put a sniffer on the outgoing request how does it look like? I am trying to do the same but via the REST API and I am having difficulty finding the necessary metadata types such as ValueSet.
    – Mossi
    Commented Nov 3, 2017 at 23:29
0

Ran out of space, hence posting as an answer.

hello Saroj. I am trying to create Picklist field using Metadata API in Salesforce. It is similar to what you have posted but getting below error message. What am I missing?

Error message: You must specify either picklist, globalPicklist, or valueSet.

//Picklist field
MetadataService.CustomField field = new MetadataService.CustomField();
field.type_x = 'Picklist';
field.Description ='Created by Metadata API';
field.Label='Metadata API';
field.FullName='WorkOrder.Metadata_API__c';

//Custom Values
List<MetadataService.CustomValue> cva = new List<MetadataService.CustomValue>();
MetadataService.CustomValue cv1 = new MetadataService.CustomValue();
cv1.fullName = 'segmentA'; 
cv1.description = 'segmentA';
cv1.isActive = true;
cva.add(cv1);
MetadataService.CustomValue cv2 = new MetadataService.CustomValue();
cv2.fullName = 'segmentB'; 
cv2.description = 'segmentB';
cv2.isActive = true;
cva.add(cv2);

//Value Set Values Definition
MetadataService.ValueSetValuesDefinition vd = new MetadataService.ValueSetValuesDefinition();
vd.value = cva;

//Value Set
MetadataService.ValueSet vs = new MetadataService.ValueSet();
vs.valueSetDefinition = vd;
field.valueSet = vs;

You must log in to answer this question.

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