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
daniel reiseldaniel reisel 

retreving from Salseforce java

hi!
for a week now i am trying to connect to my Salseforce account just to retrive my data into my java aplaction and i cant seem to get it to work.
i read all over the internet and followoed every example i could find and nothing helped.

what i did so far is:

opened a developer account.
created an app with Oath2 authentication. (i do not have a callback server so i didn't know what to use).
what I'm trying to do is this:
connect to Salseforce.
retrieve all my leads created between 2 specific dates

I'm not even sure what will be the best API for that manner...
I would really appreciate some assistance.

thank you!
​Daniel
NagaNaga (Salesforce Developers) 
Hi Daniel,

Did you try the below link, it should be helpful

 http://www.forcetree.com/2011/12/java-to-salesforcecom-integration-using.html

Please let me know if this helps

Best Regards
Naga Kiran
NaveenReddyNaveenReddy
Hi Daniel,
 If you want to connect to only your salesforce org use enterprise connection.
If you want to connect to any salesforce org you can use partner connection.
Below is the sample code in java which retrieves Lead info from Salesforce using PartnerConnection.


 
import com.sforce.soap.partner.PartnerConnection;
import com.sforce.soap.partner.QueryResult;
import com.sforce.soap.partner.sobject.*;
import com.sforce.soap.partner.*;
import com.sforce.ws.ConnectorConfig;
import com.sforce.ws.ConnectionException;
import com.sforce.soap.partner.Error;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.*;

public class PartnerCon2 {
	PartnerConnection partnerConnection = null;

	public static void main(String[] args) {
		PartnerCon2 samples = new PartnerCon2();
		if (samples.login())
			samples.querySample2();
	}

	private boolean login() {
		boolean success = false;
		try {
			ConnectorConfig config = new ConnectorConfig();
			config.setUsername("naveen.sforce@gmail.com");
			config.setPassword("Techrains@12345L1vRecUnTMw2LKabdigpr89o");
			config.setAuthEndpoint("https://login.salesforce.com/services/Soap/u/33.0");
			config.setTraceFile("traceLogs.txt");
			config.setTraceMessage(true);
			config.setPrettyPrintXml(true);
			partnerConnection = new PartnerConnection(config);
			success = true;
		} catch (ConnectionException ce) {
			ce.printStackTrace();
		} catch (FileNotFoundException fnfe) {
			fnfe.printStackTrace();
		}
		return success;
	}

	public void querySample2() {
		try {

			partnerConnection.setQueryOptions(250);

			String soqlQuery = "SELECT FirstName, LastName FROM Lead";

			QueryResult qr = partnerConnection.query(soqlQuery);

			for (SObject s : qr.getRecords()) {
				System.out.println("Lead Info :" + s.getField("FirstName")
						+ " " + s.getField("LastName"));
			}

		} catch (ConnectionException ce) {
			ce.printStackTrace();
		}
		System.out.println("\nQuery execution completed.");
	}
}


Use Partner.jar ,enterprise.jar and wsc.jar  while using above code in java.

**Mark it as solved if it solves your question.

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