Preface – This post is part of “Salesforce Integration with SOAP API” series.

In today’s post, I will explain how you can do Upsert operation using SOAP API and Java Program. Before that we should know what is so special about Upsert? Upsert will allow you to create a new record or update the existing record. Now I am sure you have this question is mind – “How Salesforce will know that the record is already there in the system?”, right!!!! Good. The answer is that you can pass the custom field which will be used to determine the presence of the existing record. So easy!!!

It is always recommended to use upsert() rather that create() because of the obvious reason i.e. upsert() will eliminate the chance of creating duplicate record in the system.

Syntax –

UpsertResult[] = connection.upsert(String externalIdFieldName, sObject[] sObjects);

Important point – 

Note: I have written a helper class which I am using to connect to Salesforce using Enterprise WSDL. To know more about that, please refer my previous post @ 

Below is the Java program –
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.UpsertResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.SObject;

public class UpsertOperation {
private EnterpriseConnection enterpriseConnection;

private UpsertOperation(){

public static void main(String[] args) {
UpsertOperation upsertOperation = new UpsertOperation();

private void doUpsertOperation(){

SObject[] allUpserts = new Account[2];

//Create an Account - Let's say this is a complete new account
Account account1 = new Account();
account1.setName("New Account");

//Create an Account - Let's say this is an already present account
Account account2 = new Account();
account2.setName("United Oil & Gas Corp");

allUpserts[0] = account1;
allUpserts[1] = account2;

//Here the comparison will happen based on the field "Account_External_ID__c"
//This field will determine whether the record is already there is the system or not
//If the record is already there, then it will do update operation, otherwise
//it will do insert operation
UpsertResult[] results = getEnterpriseConnection().upsert("Account_External_ID__c", allUpserts);

for(UpsertResult eachResult : results){
System.out.println("Upsert successful");
System.out.println((eachResult.isCreated() ? "Insert" : "Update") + " was performed!!");
for(Error eachError : eachResult.getErrors()){
System.out.println("Error occured with error message: " + eachError.getMessage());
}catch(ConnectionException e){

public EnterpriseConnection getEnterpriseConnection() {
return enterpriseConnection;

public void setEnterpriseConnection(EnterpriseConnection enterpriseConnection) {
this.enterpriseConnection = enterpriseConnection;


The output should look like –

Upsert successful
Insert was performed!!
Upsert successful
Update was performed!!
It’s really easy, right!!! If you have any question/feedback, please provide your comment.

In my next post, I will write about delete() operation. Till then enjoy.