List Salesforce Objects, Fields, References, Picklist Values using API in Java

package com.thysmichels;

import java.io.IOException;

import com.sforce.soap.partner.*;
import com.sforce.ws.*;

public class SalesforceGlobalSearch {
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		ConnectorConfig sfconfig = new ConnectorConfig();
		//Use your salesforce username = email
	    sfconfig.setUsername("username");
	    //Use your saleforce password with your security token look like: passwordjeIzBAQKkR6FBW8bw5HbVkkkk
	    sfconfig.setPassword("passwordsecuritytoken");
	 
	    PartnerConnection partnercon = null;
	    try {
	    	partnercon = Connector.newConnection(sfconfig);
	    	DescribeGlobalResult describeGlobalResult = partnercon.describeGlobal();
	    	DescribeGlobalSObjectResult[] sobjectResults = describeGlobalResult.getSobjects();
	    	if (describeGlobalResult != null)
	    	{
	    		//Object[] obj = describeGlobalResult.getSobjects();
	    		if (sobjectResults != null)
	    			for (int i = 0; i < sobjectResults.length; i++)
	    			{
	    				System.out.println("*** SObject: " + sobjectResults[i].getName() + " ***");     
	    				DescribeSObjectResult describeSObjectResult = partnercon.describeSObject(sobjectResults[i].getName());
	    				if (describeSObjectResult != null) 
	    				{
	    					Field[] fields = describeSObjectResult.getFields();
	    					PicklistEntry[] picklistValues;
	    					String[]referenceTos;
	    					for (int k = 0; k < fields.length; k++) 
	    					{
	    						System.out.println("Label: " + fields[k].getLabel() + " Name: " + fields[k].getName() + " Length: " + fields[k].getLength() + " Required Field: " + fields[k].getNillable());
	    						referenceTos = fields[k].getReferenceTo();
	    						if (referenceTos != null)
	    						{
	    							for (int j = 0; j < referenceTos.length; j++) 
	    							{
	    								System.out.println("Field Reference To: " + referenceTos[j]);
	    							}
	    						}
	    						picklistValues = fields[k].getPicklistValues();
	    						if (picklistValues != null) 
	    						{
	    							for (int j = 0; j < picklistValues.length; j++) 
	    							{
	    								if (picklistValues[j].getLabel() != null) 
	    								{
	    									System.out.println("\tItem: " + picklistValues[j].getLabel());
	    								}
	    							}
	    						}
	    					}
	    				}
	    			}
	    	} 
	    } catch (ConnectionException ce) {
	    	ce.printStackTrace();
	    }
	}
}

Output

*** SObject: Account ***
Label: Account ID Name: Id Length: 18 Required Field: false
Label: Deleted Name: IsDeleted Length: 0 Required Field: false
Label: Master Record ID Name: MasterRecordId Length: 18 Required Field: true
Field Reference To: Account
Label: Account Name Name: Name Length: 255 Required Field: false
Label: Account Type Name: Type Length: 40 Required Field: true
	Item: Prospect
	Item: Customer - Direct
	Item: Customer - Channel
	Item: Channel Partner / Reseller
	Item: Installation Partner
	Item: Technology Partner
	Item: Other
Label: Parent Account ID Name: ParentId Length: 18 Required Field: true
Field Reference To: Account

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s