+ Start a Discussion
Madhu007Madhu007 

Updating Picklist values in the Field Not a record

Hi, I have requirement like this.
I have a pick list field with few values.i want to add some more picklist value to this field from excel sheet. Not like this object>field>edit...
Is there any chance to do this..

Thanks in advance.
NaveenReddyNaveenReddy
Hi,
This can be possible by using salesforce  metadata API. Please find sample code below.
 
CustomField c = new CustomField();
		c.setType(type);
		c.setPicklist(picklist);
		c.setFullName(ObjectName.Fieldname);
		SaveResult[] results = metadataConnection
				.updateMetadata(new Metadata[] { c });
Regards,
Naveen
http://www.autorabit.com
 
Sfdc CloudSfdc Cloud
Hi Madhu,

Same scenarion has been posted on community.We cant acheive this directly with code,u have to call  external webservices which will call back metadataapi at salesforce end to add/updxate the picklist value.

Please refer below link for your issue.That would be helpful 
https://developer.salesforce.com/forums?id=906F00000008yw2IAA

Thanks :)

 
Madhu007Madhu007
Hi Naveen, Can you please elaborate your sample code. Can i use this code in apex class directly. Thanks. Madhu.
NaveenReddyNaveenReddy
Hi,

 The sample code is from java. I have used salesforce provided Metadata API and Partner API.

The complete code is provided below.
 
//To following code creates a metadata connection
import com.sforce.soap.metadata.*;
import com.sforce.soap.partner.LoginResult;
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
/**
* Login utility.
*/
public class MetadataLoginUtil {
public static MetadataConnection login() throws ConnectionException {
  final String USERNAME = ""username@gmail.com"";
  // This is only a sample. Hard coding passwords in source files is a bad practice.
  final String PASSWORD = ""Password+SecurityToken"";
  final String URL = ""https://login.salesforce.com/services/Soap/u/31.0"";

  //Quick Start Step 3: Walk through the Java Sample Code
  final LoginResult loginResult = loginToSalesforce(USERNAME, PASSWORD, URL);
  //System.out.println(loginResult);
  return createMetadataConnection(loginResult);
}
private static MetadataConnection createMetadataConnection(
   final LoginResult loginResult) throws ConnectionException {
  final ConnectorConfig config = new ConnectorConfig();
  config.setServiceEndpoint(loginResult.getMetadataServerUrl());
  config.setSessionId(loginResult.getSessionId());
  return new MetadataConnection(config);
}
private static LoginResult loginToSalesforce(
   final String username,
   final String password,
   final String loginUrl) throws ConnectionException {
  final ConnectorConfig config = new ConnectorConfig();
  config.setAuthEndpoint(loginUrl);
  config.setServiceEndpoint(loginUrl);
  config.setManualLogin(true);
  return (new PartnerConnection(config)).login(username, password);
}

}

//The following code creates a custom field using  metadata conncetion and createMetadata() method.

import java.util.ArrayList;
import java.util.concurrent.ForkJoinPool.ManagedBlocker;
import com.sforce.async.Error;
import com.sforce.soap.metadata.*;
import com.sforce.soap.metadata.FieldType;
import com.sforce.soap.metadata.Package;
import com.sforce.soap.metadata.SaveResult;
import com.sforce.soap.partner.*;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import com.sforce.ws.wsdl.SfdcApiType;
/**
* Login utility.
*/
public class PartnerLoginUtil {

public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  try {
   PartnerLoginUtil p= new PartnerLoginUtil( );
   p.createCustomExtField(""CustomObjectName"");

  } catch (ConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
}

private void createCustomExtField(String name) {
  // TODO Auto-generated method stub
String country[] = {"US","UK"};
CustomField c = new CustomField();
 c.setType(FieldType.Picklist);
 c.setPicklist(country);
  cs.setFullName(name+"".CustExtField__c"");
  cs.setLabel(""CustExtField"");
    try {
   MetadataConnection metadataConnection = MetadataLoginUtil.login();
   SaveResult[] results = metadataConnection
     .updateMetadata(new Metadata[] {cs});

   for (SaveResult r : results) {
    if (r.isSuccess()) {
     System.out.println(""Created component: "" + r.getFullName());
    } else {
     System.out
     .println(""Errors were encountered while creating ""
       + r.getFullName());
     for (com.sforce.soap.metadata.Error e : r.getErrors()) {
      System.out.println(""Error message: "" + e.getMessage());
      System.out.println(""Status code: "" + e.getStatusCode());
     }
    }
   }
  } catch (ConnectionException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

}

}

*Mark it as solved if it finds solution for your question.
Regards,
Naveen
http://www.autorabit.com
Madhu007Madhu007
Hi Naveen, Thank you for sharing the sample code. I did not get any idea how to use this java sample code to update pick-list values. I have also verified so many links in the online. All sites given same information like you shared here. It seems we can not use metadata from our apex class. we should update from outside(other application). Really i am so confusing. How we call metadata api. We can not call it from our apex class. Please can you give the direction to how to do this. Thank you in advance.
NaveenReddyNaveenReddy
Hi Madhu,
 We cannot use metadata API with apex code. Instead use force.com IDE or Ant Migration tool for your requirement they are based on metadata API only.
 Please go through the below document it gives clear idea on how to use metadata API.

http://www.salesforce.com/us/developer/docs/api_meta/api_meta.pdf 

Regards,
Naveen.
http://www.autorabit.com