Wednesday, February 25, 2015

Salesforce Integration with SOAP API - Part 9 - Upsert operation using Java program


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 ConnectionHelper.java 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 - UpsertOperation.java
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;
import com.sforce.ws.ConnectionException;


public class UpsertOperation {
    private EnterpriseConnection enterpriseConnection;
    
    private UpsertOperation(){
        setEnterpriseConnection(ConnectionHelper.doConnect());
    }
    
    public static void main(String[] args) {
        UpsertOperation upsertOperation = new UpsertOperation();
        upsertOperation.doUpsertOperation();
    }
    
    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");
        account1.setAccountNumber("11111");
        account1.setRating("Hot");
        account1.setPhone("888888");
        account1.setAccount_External_ID__c("11111");
        
        //Create an Account - Let's say this is an already present account
        Account account2 = new Account();
        account2.setName("United Oil & Gas Corp");
        account2.setAccountNumber("MN355118");
        account2.setRating("Hot");
        account2.setPhone("888888");
        account2.setAccount_External_ID__c("CD355118");
        
        allUpserts[0] = account1;
        allUpserts[1] = account2;
        
        try{
            //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){
                if(eachResult.isSuccess()){
                    System.out.println("Upsert successful");
                    System.out.println((eachResult.isCreated() ? "Insert" : "Update") + " was performed!!");
                }else{
                    for(Error eachError : eachResult.getErrors()){
                        System.out.println("Error occured with error message: " + eachError.getMessage());
                    }
                }
            }
        }catch(ConnectionException e){
            e.printStackTrace();
        }
    }

    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.

0 comments:

Post a Comment