Last active
August 29, 2015 14:20
-
-
Save victorabraham/368bf9f0990b4ad8e89f to your computer and use it in GitHub Desktop.
This code sample demonstrates, how to connect to salesforce using partner jar generated from partner WSDL using WSC jar provided by salesforce.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package partner; | |
import com.sforce.soap.partner.Connector; | |
import com.sforce.soap.partner.DeleteResult; | |
import com.sforce.soap.partner.PartnerConnection; | |
import com.sforce.soap.partner.QueryResult; | |
import com.sforce.soap.partner.SaveResult; | |
import com.sforce.soap.partner.Error; | |
import com.sforce.soap.partner.sobject.SObject; | |
import com.sforce.ws.ConnectionException; | |
import com.sforce.ws.ConnectorConfig; | |
public class PartnerExample { | |
//Hardcoded username and password | |
static final String USERNAME = "USERNAME here"; | |
static final String PASSWORD = "Password + Security token here"; | |
static PartnerConnection connection; | |
public static void main(String[] args) { | |
//Creating connector config instance with username and password | |
ConnectorConfig config = new ConnectorConfig(); | |
config.setUsername(USERNAME); | |
config.setPassword(PASSWORD); | |
try { | |
//Passing configuration to connection object | |
connection = Connector.newConnection(config); | |
//If connection is successful session Id can be obtained with below line | |
System.out.println("SessionId: "+config.getSessionId()); | |
//Once connection is established records can be queried as below | |
QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName FROM Contact LIMIT 5"); | |
//Query results can be be convereted to sobject array using below method | |
SObject[] sObjList = queryResults.getRecords(); | |
for(SObject so:sObjList){ | |
System.out.println("Query Result"+so); | |
} | |
//New records are created in salesforce using below lines, | |
SObject[] records = new SObject[1]; | |
SObject so = new SObject(); | |
so.setType("Account"); | |
so.setField("Name", "SOAP Testing Account"); | |
records[0] = so; | |
SaveResult[] saveResults = connection.create(records); | |
System.out.println("Account Creation"+saveResults); | |
for(SaveResult sr:saveResults){ | |
System.out.println("Account Creation"+sr); | |
} | |
} catch (ConnectionException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment