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
Sharankumar DesaiSharankumar Desai 

Metadata wsdl API to create picklist field with picklist values

Hi All,

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 am facing using to create a picklist field and add picklist values to it.
Below is the cource code i have written i am not able to understand 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 .

SALESFORCE VERSION : 39.0
NOTE: I am using Java appilcation for doing this job

Appreciate your help !!
Gaurish Gopal GoelGaurish Gopal Goel
Hi Sharan,

You can make an HTTP Callout like the below one. You just need to change the XML and find the appropriate tags to set the picklist values and other properties. I used this to create custom fields and assignment rules. I hope it will help you.
HTTP h = new HTTP();
HTTPRequest req = new HTTPRequest();
req.setMethod('POST');
req.setHeader('Content-Type', 'text/xml');
req.setHeader('SOAPAction', 'create');

String b = '<?xml version="1.0" encoding="UTF-8"?>';
b += '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
b += '<soapenv:Header>';
b += '<ns1:SessionHeader soapenv:mustUnderstand="0" xmlns:ns1="http://soap.sforce.com/2006/04/metadata">';
b += '<ns1:sessionId>' + UserInfo.getSessionId() + '</ns1:sessionId>';
b += '</ns1:SessionHeader>';
b += '</soapenv:Header>';
b += '<soapenv:Body>';
b += '<create xmlns="http://soap.sforce.com/2006/04/metadata">';
b += '<metadata xsi:type="ns2:AssignmentRule" xmlns:ns2="http://soap.sforce.com/2006/04/metadata">';
b += '<fullName>samplerule</fullName>';
b += '<active>false</active>';
b += '<ruleEntry>';
b += '<assignedTo>testUser@org.com</assignedTo>';
b += '<assignedToType>User</assignedToType>';
b += '<criteriaItems>';
b += '<field>Case.IsEscalated</field>';
b += '<operation>equals</operation>';
b += '<value>True</value>';
b += '</criteriaItems>';
b += '</ruleEntry>';
b += '</metadata>';
b += '</create>';
b += '</soapenv:Body>';
b += '</soapenv:Envelope>';

req.setBody(b);
req.setCompressed(false);
req.setEndpoint('https://gaurish-dev-ed.my.salesforce.com/services/Soap/m/25.0');
HTTPResponse resp = h.send(req);
System.debug(resp.getBody());
PLEASE MARK IT AS THE BEST ANSWER IF IT WORKS FOR YOU.

Thanks,
Gaurish
http://www.gaurishgoel.com (http://www.gaurishgoel.com" target="_blank)
Sharankumar DesaiSharankumar Desai
Hi Gaurish,

I am not looking for Callouts, thats my question that these tags to set Picklistvalue is not available.

I have generated jar file from the wsdl  and using in my java application.