Building Dynamic Queries using Salesforce Java API

package com.thysmichels;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

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


public class SearchSalesforce 
{

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException 
	{
		// TODO Auto-generated method stub
		//Create a new connectionconfig to your Salesforce Org
				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();
		    	System.out.println("*** Salesforce Objects ***");     
		    	for (int i = 0; i < sobjectResults.length; i++)
		    	{
		    		System.out.print("Object Number: [" + i + "] " +  sobjectResults[i].getName() + "\t" + "|" + "");     
		    	}
				System.out.println("Enter Salesforce Object Number: ");
				BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
				String name = reader.readLine();
				DescribeSObjectResult describeSObjectResult = partnercon.describeSObject(sobjectResults[Integer.parseInt(name)].getName());
				System.out.println("*** " + sobjectResults[Integer.parseInt(name)].getName() +"  Fields ***");
				Field[] fields = describeSObjectResult.getFields();
				if (describeSObjectResult != null) 
				{
					for (int k = 0; k < fields.length; k++) 
					{
						System.out.print("Column Number: ["+ k + "]  " + fields[k].getName() + "\t" + "|" + "");

					}
				}
				
				System.out.println("\n" + "How many columns do you want to view: ");
				String times = reader.readLine();
				String concatfield = "";
				String arrfields[] = new String[Integer.parseInt(times)];
				for (int m=0;m < Integer.parseInt(times);m++)
				{
					System.out.println("Enter the column number");
					String field = reader.readLine();
					if (m==0)
					{
						concatfield += fields[Integer.parseInt(field)].getName();
						arrfields[m] = fields[Integer.parseInt(field)].getName();
					}
					else
					{
						concatfield += "," + fields[Integer.parseInt(field)].getName();
						arrfields[m] = fields[Integer.parseInt(field)].getName();
					}
				}
			//for (int col=0; col  0) 
			      {
			        for (SObject s: queryResults.getRecords()) 
			        {
			        	
			        	for (int col=0; col < arrfields.length; col++)
			        	{
			              System.out.print(arrfields[col] + ": "+ s.getField(arrfields[col]) + "\t" + "|" + "\t");
			        	}
			        	System.out.println("\n");
			        }
			      }
		}
	    catch(ConnectionException e)
	    {
	        e.printStackTrace();
	    }
	        
	}
}

Output

*** Salesforce Objects ***
Object Number: [0] Account	|Object Number: [1] AccountContactRole	|Object Number: [2] AccountFeed	|Object Number: [3] AccountHistory
0
*** Account  Fields ***
Column Number: [0]  Id	|Column Number: [1]  IsDeleted	|Column Number: [2]  MasterRecordId	|Column Number: [3]  Name
How many columns do you want to view: 
3
Enter the column number
0
Enter the column number
3
Enter the column number
19
*** RESULT FROM SALESFORCE DYNAMIC QUERY ***
Id: 001F000000gMPyrIAG	|	Name: Test2	|	Website: null	|	

Id: 001A000000JYp5yIAD	|	Name: GenePoint	|	Website: www.genepoint.com	|	

Id: 001A000000JYp5zIAD	|	Name: United Oil & Gas, UK	|	Website: http://www.uos.com	|	

Id: 001A000000JYp60IAD	|	Name: United Oil & Gas, Singapore	|	Website: http://www.uos.com	|	

Id: 001A000000JYp61IAD	|	Name: Edge Communications	|	Website: http://edgecomm.com	|
About these ads

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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 235 other followers

%d bloggers like this: