Tuesday, February 17, 2015

Salesforce Integration with SOAP API - Part 4 - Java program for connecting to Salesforce using SOAP API and Enterprise WSDL


Preface - This post is part of "Salesforce Integration with SOAP API" series.

In my last post, I have explained how you can configure your Eclipse so that you can start writing your first Java Program to connect to Salesforce using SOAP API. Below is the link of that post -

Salesforce Integration with SOAP API - Part 3 - Configure Eclipse for Enterprise WSDL

Here comes the Java Helper Class to connect to Salesforce using SOAP API.
ConnectionHelper.java:
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;


public class ConnectionHelper {
    static final String USERNAME = "<SALESFORCE USERNAME>";
    static final String PASSWORD = "<SALESFORCE PASSWORD><SECURITY TOKEN>";
    
    public static EnterpriseConnection doConnect(){
        EnterpriseConnection connection = null;
        ConnectorConfig config = new ConnectorConfig();
        config.setUsername(USERNAME);
        config.setPassword(PASSWORD);
        //Add proxy details if requoired
        config.setProxy("<PROXY URL>",<PROXY PORT>);
        config.setProxyPassword("<PROXY PASSWORD>");
        
        try{
            connection = Connector.newConnection(config);
            
            System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
            System.out.println("Service EndPoint: "+config.getServiceEndpoint());
            System.out.println("Username: "+config.getUsername());
            System.out.println("SessionId: "+config.getSessionId());
        }catch (ConnectionException e1) {
            e1.printStackTrace();
        }
        
        return connection;
    }
}

Now I will write the main method to call the doConnect method for connecting to Salesforce. The code is given below -
CheckConnectivity.java
public class CheckConnectivity {
    public static void main(String[] args) {
        ConnectionHelper.doConnect();
    }

}

Output -
You should see an output like this below -
Auth EndPoint: https://login.salesforce.com/services/Soap/c/32.0/0DF900000004uQh
Service EndPoint: https://ap1.salesforce.com/services/Soap/c/32.0/00D90000000s4Hg/0DF900000004uQh
Username: <YOUR USER NAME>
SessionId: 00D90000000s4Hg!ARkAQNCqDG9j80Xm52KDgbN0aZSuQLP0Ql1q0XedDIZ8jymlHtG3Y2KvIEJ04A0KJqPBgqhVzcNvQZ.Wy4k7ugk_Wt0RKACx

That's all. From my next posts, I will start explaining all the CRUD operations using SOAP API. .
Till then any feedback, please let me know. Thanks.

0 comments:

Post a Comment