+ Start a Discussion
Jeff C.ax327Jeff C.ax327 

Metadata API -- "set" methods unavailable? (setFullname, setPicklistValues, etc)

We need the ability to programmatically add new picklist values from our website to a Salesforce object.

I am new to the Metadata API, but it sure looks like that's the way to do it.

So I downloaded and added the Metadata API web service to my c#.net application.

 

There is plenty of sample code that demonstrates how to get up and running.

I even found sample code on how to modify picklists.

However, none of the "set" methods are available in the library and they are flagged as unrecognized.

Below, all references in red font are syntax errors and the methods cannot be resolved.

 

static void CreatePicklist() { Picklist expenseStatus = new Picklist(); PicklistValue unsubmitted = new PicklistValue(); unsubmitted.setFullName("Unsubmitted"); PicklistValue submitted = new PicklistValue(); submitted.setFullName("Submitted"); expenseStatus.setPicklistValues(new PicklistValue[] {unsubmitted, submitted}); CustomField expenseStatusField = new CustomField(); expenseStatusField.setFullName("ExpenseReport__c.ExpenseStatus__c"); expenseStatusField.setLabel("Expense Report Status"); expenseStatusField.setType(FieldType.Picklist); expenseStatusField.setPicklist(expenseStatus); metaService.create(new Metadata[] { expenseStatusField }); }

 

 

I downloaded the Metadata API from an administrator-privileged account on both production and sandbox instances, but nothing helps.  Any advice would be most appreciated.


Thanks!

 

SuperfellSuperfell

.NET does not use setters and getter, instead it uses .NET properties, so

foo.setName("bob");

should be

foo.Name = "bob"; 

Jeff C.ax327Jeff C.ax327

geez, well that's simple... :-)

 

Outstanding, thank you Simon!

 

Jeff C.ax327Jeff C.ax327

What methods are used to insert a new value in a picklist?

When I try to use:

   AsyncResult result = create(new Metadata[] { picklistField })[0];

the result error message is "There is already a field named ExpenseStatus on TestCustomObject."

This certainly makes sense, because the field does already exist.

However, I don't follow how to use the  update  method to add a picklist value either, the parameter types are totally different and it doesn't make sense to me.

Is it possible to use the Metadata API to insert a new picklist value into an existing picklist?

Jeff C.ax327Jeff C.ax327

Here is the entire procedure... I just threw this together and I'm sure there are better ways of doing some of this, but I thought a complete example might help someone tell me where I'm going wrong.

 

public void InsertPicklistValue(string objectName, string picklistFieldName, string valueToInsert) { try { DescribeSObjectResult descSObjectRslt = c_sfEnterprise.describeSObject(objectName); if (descSObjectRslt != null) { Field[] fields = descSObjectRslt.fields; if (fields != null) { foreach (Field field in fields) { if (field.name == picklistFieldName) { PicklistEntry[] picklistValues = field.picklistValues; if (picklistValues != null) { List<PicklistValue> pVals = new List<PicklistValue>(); PicklistValue newValue; foreach (PicklistEntry picklistValue in picklistValues) { string sValue; if (picklistValue.label != null) sValue = picklistValue.label; else sValue = picklistValue.value; newValue = new PicklistValue(); newValue.fullName = sValue; newValue.@default = picklistValue.defaultValue; pVals.Add(newValue); } newValue = new PicklistValue(); newValue.fullName = valueToInsert; pVals.Add(newValue); Picklist pList = new Picklist(); pList.picklistValues = pVals.ToArray(); CustomField picklistField = new CustomField(); picklistField.fullName = objectName + "." + picklistFieldName; picklistField.label = field.label; picklistField.type = FieldType.Picklist; picklistField.picklist = pList; AsyncResult result = create(new Metadata[] { picklistField })[0]; while (!result.done) { System.Threading.Thread.Sleep(result.secondsToWait * 1000); result = checkStatus(new string[] { result.id })[0]; } if (result.state == AsyncRequestState.Error) Console.WriteLine("Error : {0} {1}", result.statusCode, result.message); else { Console.WriteLine("Picklist updated successfully\n\n" + result.message); } break; } } } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("\nFailed to get " + objectName + " description, error message was: \n " + ex); } }