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
Jerun JoseJerun Jose 

Update picklist values using Metadata API

Hi,

 

Can someone help me out with Metadata API ? I need to figure out a way to change values for a picklist field using apex. I thought that an update() call using the Metadata API should be the right direction, but having a hard time putting together the format of the request.

Also, can we do picklist value changes in Production using the update() call? Based on the post at
http://boards.developerforce.com/t5/Apex-Code-Development/CRUD-based-development-Metadata-API/m-p/193777#M32258
I believe we will need a deploy() call for any deployment to production.

Bindhyachal Kumar SinghBindhyachal Kumar Singh

Hi Jerun Jose,

 

You can use following code if you are calling salesforce from JAVA.

public void setPicklistValues() {
// Create a picklist
Picklist expenseStatus = new Picklist();
PicklistValue unsubmitted = new PicklistValue();
unsubmitted.setFullName("Unsubmitted");
PicklistValue submitted = new PicklistValue();
submitted.setFullName("Submitted");
PicklistValue approved = new PicklistValue();
approved.setFullName("Approved");
126
Metadata Types Picklist (Including Dependent Picklist)
PicklistValue rejected = new PicklistValue();
rejected.setFullName("Rejected");
expenseStatus.setPicklistValues(new PicklistValue[]{unsubmitted, submitted, approved, rejected});
CustomField expenseStatusField = new CustomField();
expenseStatusField.setFullName("ExpenseReport__c.ExpenseStatus__c");
expenseStatusField.setLabel("Expense Report Status");
expenseStatusField.setType(FieldType.Picklist);
expenseStatusField.setPicklist(expenseStatus);

try {
UpdateMetadata ut = new UpdateMetadata();
// Set CurrentName of existing picklist field
ut.setCurrentName("ExpenseReport__c.ExpenseStatus__c");
ut.setMetadata(expenseStatusField);
AsyncResult[] asyncResultsupdate = metadataConnection.update(newUpdateMetadata[] {ut});  
} catch (ConnectionException ce) {
ce.printStackTrace();
}
}

 

In the above code we are updating the field ExpenseStatus__c of Object ExpenseReport__c.

 

ExpenseReport__c.ExpenseStatus__c

 

If you are using Apex method then i have already posted the code on this discussion board.

 

http://boards.developerforce.com/t5/Apex-Code-Development/Add-new-Picklist-value-with-trigger/m-p/585055#M107031

Sharankumar DesaiSharankumar Desai
Hello Bindhyachal Kumar Singh,

I am not able to find the method setPicklist(expenseStatus) as you used in the below line.

expenseStatusField.setPicklist(expenseStatus);

Is it something got changed in the latest releases.  Appreciate your help.