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
CP_PDCP_PD 

Adding a picklist value via Salesforce API Version 37 with C#/.NET

Some years ago, I created a Synkronization between Dynamics NAV and Salesforce via API version 24, and works fine :-)
Now I ned to call Salesforce API Version 37. I have downloaded sforce and metadata WSDL files, and everything is ok - except for one thing: Addiing a piclist value to a picklist.

API Version 24
In this version I read the picklist value one by one and created a new picklist. Finaly I added the piclist like:
 
// Update the existing picklist.
                        UpdateMetadata umd = new UpdateMetadata();
                        umd.currentName = tableName + "." + fieldName;
                        umd.metadata = picklistField;
                        MetadataService mds = new MetadataService();
                        mds.SessionHeaderValue = new metadata.SessionHeader();
                        mds.SessionHeaderValue.sessionId = sessionID;
                        mds.Url = metadataURL;
                        AsyncResult result = mds.update(new UpdateMetadata[] { umd })[0];

API Version 37
WSDL metadata no longer have the type UpdateMetadata, from where I added the picklist in API version 24. I can read elements of the picklist, but I cannot add the list.

I have "asked Google", if he could help me, but I got no response.
(for API Version 37)

I you have a piece of code adding a picklist though API 37, I would be greatful.
 
Best Answer chosen by CP_PD
UC InnovationUC Innovation
Give the below a try.  The example is based on updating the Picklist__c field on the Band__c custom object:
// Create a picklist
Picklist status = new Picklist();

PicklistValue unsubmitted = new PicklistValue();
unsubmitted.fullName = "Unsubmitted";

PicklistValue submitted = new PicklistValue();
submitted.fullName = "Submitted";

status.picklistValues = new PicklistValue[] { unsubmitted, submitted };
CustomField statusField = new CustomField();
statusField.fullName = "Band__c.Picklist__c";
statusField.label = "Picklist";
statusField.typeSpecified = true;
statusField.type = FieldType.Picklist;
statusField.picklist = status;

try
{
    SaveResult[] saveResults = metadataApi.updateMetadata(new Metadata[] { statusField });
}
catch (Exception ex)
{
    // Handle errors...
}

The above will replace the existing picklist values with "Unsubmitted" and "Submitted".  Hope that helps.
 

All Answers

UC InnovationUC Innovation
Give the below a try.  The example is based on updating the Picklist__c field on the Band__c custom object:
// Create a picklist
Picklist status = new Picklist();

PicklistValue unsubmitted = new PicklistValue();
unsubmitted.fullName = "Unsubmitted";

PicklistValue submitted = new PicklistValue();
submitted.fullName = "Submitted";

status.picklistValues = new PicklistValue[] { unsubmitted, submitted };
CustomField statusField = new CustomField();
statusField.fullName = "Band__c.Picklist__c";
statusField.label = "Picklist";
statusField.typeSpecified = true;
statusField.type = FieldType.Picklist;
statusField.picklist = status;

try
{
    SaveResult[] saveResults = metadataApi.updateMetadata(new Metadata[] { statusField });
}
catch (Exception ex)
{
    // Handle errors...
}

The above will replace the existing picklist values with "Unsubmitted" and "Submitted".  Hope that helps.
 
This was selected as the best answer
CP_PDCP_PD
Thanks for your reply :-)

Unfortunately, it does not work :-(

I create the picklist dynamically.
I get this error, when calling Salesforce API:

"Must specify a FieldType"
 But I have specified a FieldType. (see my code below)
public void ChangePicklist(PICKLIST_ACTIONS plAction, string tableName, string fieldName, string picklistValue) {
            // Load describe info for the object.
            DescribeSObjectResult dor = binding.describeSObject(tableName);

            if (dor != null) {
                Field[] fields = dor.fields;
                foreach (Field field in fields) {
                    if (field.name == fieldName) {
                        // Get the array of existing picklist entries.
                        sforce.PicklistEntry[] picklistEntries = field.picklistValues;
                        // Recreate the entire list of existing picklist values...
                        List<PicklistValue> pVals = new List<PicklistValue>();
                        PicklistValue pVal;
                        foreach (sforce.PicklistEntry picklistEntry in picklistEntries) {
                            bool updatePl = true;
                            if ((plAction == PICKLIST_ACTIONS.Delete) & (picklistEntry.value == picklistValue))
                                updatePl = false;
                            if (updatePl) {
                                pVal = new PicklistValue();
                                pVal.@default = picklistEntry.defaultValue;
                                if (picklistEntry.label != null)
                                    pVal.fullName = picklistEntry.label;
                                else
                                    pVal.fullName = picklistEntry.value;
                                pVals.Add(pVal);
                            }
                        }
                        if (plAction == PICKLIST_ACTIONS.Create) {
                            // Add the NEW value...
                            pVal = new PicklistValue();
                            pVal.fullName = picklistValue;
                            pVals.Add(pVal);
                        }
                        // Save the values to a new picklist.
                        Picklist pList = new Picklist();
                        pList.picklistValues = pVals.ToArray();
                        CustomField picklistField = new CustomField();
                        picklistField.fullName = tableName + "." + fieldName;
                        //picklistField.description = fieldName;
                        picklistField.label = field.label;
                        picklistField.type = FieldType.Picklist;
                        picklistField.picklist = pList;
                        picklistField.inlineHelpText = field.inlineHelpText;

                        MetadataService mds = new MetadataService();
                        mds.SessionHeaderValue = new metadata.SessionHeader();
                        mds.SessionHeaderValue.sessionId = sessionID;
                        mds.Url = metadataURL;

                        metadata.SaveResult[] result = mds.updateMetadata(new Metadata[] { picklistField });
                    }
                }
            }
        }

 
CP_PDCP_PD
I forgot the line

statusField.typeSpecified = true;

hat you mensioned. And now it works fine :-)

Thank you
UC InnovationUC Innovation
No problem. Great to hear that it worked for you!