1. Download the WSDL file in the Salesforce platform
Once we have created the objects that we need to use in Salesforce, we want to read and write records to objects in other applications, the first thing we need is the permission of our Salesforce platform. Log in to your salesforce and download the WSDL file.
In the top right-hand corner, click Settings-"Application Settings-" development--"API.
If you are in English, then click Your Name-"Setup-" App Setup--"Develop--" API.
After you reach the following page, download the three WSDL files in the diagram.
2. Download and build the WSC jar, then compile the corresponding WSDL file into the corresponding jar
The official method here is to download the Salesforces WSC project in GitHub HTTPS://GITHUB.COM/FORCEDOTCOM/WSC also have WSC to generate the WSDL file in the Jar tutorial, But when I execute tomvn clean package is unable to clean up the WSC project, because when downloading some packages, the link is the external network, although there is FQ, but the update fails, and the speed is very slow, and finally only the failure ends. So I went to find another way. The principle is to obtain a WSC jar, then build the jar file of the WSDL file through the command of CMD, and then introduce these jar files into the Java project, then you can read and write the objects in salesforce.
Download the jar file of WSC from the link http://code.google.com/p/sfdc-wsc/downloads/list. I downloaded the wsc-23.jar file. If you can't open the website, you need to turn over the wall. If you can't continue downloading, right-click to download with Xunlei. Mine is downloaded with Xunlei. If there are members, it can take up to ten seconds.
After downloading, the WSDL file is compiled into the corresponding jar. Here I use the windows system. Put the jar package just downloaded and the three WSDL files in the same folder, open the CMD black command window, and first use the CD command to enter the corresponding folder. Next, enter the following in the CMD command window:
java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar
java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc partner.wsdl partner.jar
java -classpath wsc-23.jar com.sforce.ws.tools.wsdlc metadata.wsdl metadata.jar
Here we need to install the Java JDK, originally developed Java program to read and write Salesforce objects and data, I will be when everyone has installed the JDK. If you do not have a problem, then your folder should have 3 more jar files, and then the entire folder will become:
3. Creating a program and referencing an external jar file
Ready, and then you want to develop any program, Web,server, Android (Android and iOS have another SDK, no need to use this method, personal understanding) and so on.
Creating Java programs and referencing jar external packages I don't explain that the jar to be applied is the 4 jars in the folder, yes, Wsc-23.jar
Package WSC;
import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.DeleteResult;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
public class Main {
Static final string username = "your username"; / / username in salesforce account
Static final string password = "your-password & security-token"; / / the password is a little special. You need to add a security token after the password
static EnterpriseConnection connection;
public static void main(String[] args) {
ConnectorConfig config = new ConnectorConfig();
config.setUsername(USERNAME);
config.setPassword(PASSWORD);
Try {
connection = Connector.newConnection(config);
//Add, delete, modify and check
queryContacts();
createAccounts();
updateAccounts();
deleteAccounts();
} catch (ConnectionException e1) {
e1.printStackTrace();
}
}
// queries and displays the 5 newest contacts
private static void queryContacts() {
System.out.println("Querying for the 5 newest Contacts...");
Try {
// query for the 5 newest contacts
QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
"FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5");
if (queryResults.getSize() > 0) {
for (int i=0;i<queryResults.getRecords().length;i++) {
// cast the SObject to a strongly-typed Contact
Contact c = (Contact)queryResults.getRecords()[i];
System.out.println("Id: " + c.getId() + " - Name: "+c.getFirstName()+" "+
c.getLastName()+" - Account: "+c.getAccount().getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// create 5 test Accounts
private static void createAccounts() {
System.out.println("Creating 5 new test Accounts...");
Account[] records = new Account[5];
Try {
// create 5 test accounts
for (int i=0;i<5;i++) {
Account a = new Account();
a.setName("Test Account "+i);
records[i] = a;
}
// create the records in Salesforce.com
SaveResult[] saveResults = connection.create(records);
// check the returned results for any errors
for (int i=0; i< saveResults.length; i++) {
if (saveResults[i].isSuccess()) {
System.out.println(i+". Successfully created record - Id: " + saveResults[i].getId());
} else {
Error[] errors = saveResults[i].getErrors();
for (int j=0; j< errors.length; j++) {
System.out.println("ERROR creating record: " + errors[j].getMessage());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// updates the 5 newly created Accounts
private static void updateAccounts() {
System.out.println("Update the 5 new test Accounts...");
Account[] records = new Account[5];
Try {
QueryResult queryResults = connection.query("SELECT Id, Name FROM Account ORDER BY " +
"CreatedDate DESC LIMIT 5");
if (queryResults.getSize() > 0) {
for (int i=0;i<queryResults.getRecords().length;i++) {
// cast the SObject to a strongly-typed Account
Account a = (Account)queryResults.getRecords()[i];
System.out.println("Updating Id: " + a.getId() + " - Name: "+a.getName());
// modify the name of the Account
a.setName(a.getName()+" -- UPDATED");
records[i] = a;
}
}
// update the records in Salesforce.com
SaveResult[] saveResults = connection.update(records);
// check the returned results for any errors
for (int i=0; i< saveResults.length; i++) {
if (saveResults[i].isSuccess()) {
System.out.println(i+". Successfully updated record - Id: " + saveResults[i].getId());
} else {
Error[] errors = saveResults[i].getErrors();
for (int j=0; j< errors.length; j++) {
System.out.println("ERROR updating record: " + errors[j].getMessage());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// delete the 5 newly created Account
private static void deleteAccounts() {
System.out.println("Deleting the 5 new test Accounts...");
String[] ids = new String[5];
Try {
QueryResult queryResults = connection.query("SELECT Id, Name FROM Account ORDER BY " +
"CreatedDate DESC LIMIT 5");
if (queryResults.getSize() > 0) {
for (int i=0;i<queryResults.getRecords().length;i++) {
// cast the SObject to a strongly-typed Account
Account a = (Account)queryResults.getRecords()[i];
System.out.println("Updating Id: " + a.getId() + " - Name: "+a.getName());
// modify the name of the Account
a.setName(a.getName()+" -- UPDATED");
records[i] = a;
}
}
// update the records in Salesforce.com
SaveResult[] saveResults = connection.update(records);
// check the returned results for any errors
for (int i=0; i< saveResults.length; i++) {
if (saveResults[i].isSuccess()) {
System.out.println(i+". Successfully updated record - Id: " + saveResults[i].getId());
} else {
Error[] errors = saveResults[i].getErrors();
for (int j=0; j< errors.length; j++) {
System.out.println("ERROR updating record: " + errors[j].getMessage());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// delete the 5 newly created Account
private static void deleteAccounts() {
System.out.println("Deleting the 5 new test Accounts...");
String[] ids = new String[5];
Try {
QueryResult queryResults = connection.query("SELECT Id, Name FROM Account ORDER BY " +
"CreatedDate DESC LIMIT 5");
if (queryResults.getSize() > 0) {
for (int i=0;i<queryResults.getRecords().length;i++) {
// cast the SObject to a strongly-typed Account
Account a = (Account)queryResults.getRecords()[i];
// add the Account Id to the array to be deleted
ids[i] = a.getId();
System.out.println("Deleting Id: " + a.getId() + " - Name: "+a.getName());
}
}
// delete the records in Salesforce.com by passing an array of Ids
DeleteResult[] deleteResults = connection.delete(ids);
// check the results for any errors
for (int i=0; i< deleteResults.length; i++) {
if (deleteResults[i].isSuccess()) {
System.out.println(i+". Successfully deleted record - Id: " + deleteResults[i].getId());
} else {
Error[] errors = deleteResults[i].getErrors();
for (int j=0; j< errors.length; j++) {
System.out.println("ERROR deleting record: " + errors[j].getMessage());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
For example, if you need to use a new object, such as a position object that I created myself, you can find the API name for the object in the object's detail page, and the name of the field API in the object, as
After finding the corresponding API name, if you need to create a Position record, then you can do this new object position__c a = new position__c (); The value of the Object field can then be assigned through the set extension method.
Web server services for Java development through the SOAP API and the metadata API in Salesforce