Monday, February 23, 2015

Salesforce Integration with SOAP API - Part 8 - Update Account records with Java program

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

In today's post, we will go through the code to understand how we can update records using SOAP API and Java program. Before going into the Java program, I would request you guys to check the documentation @
Update core call with SOAP API
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 @ 
Now below code will update Account record - 
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.ws.ConnectionException;


public class UpdateAccount {
    private EnterpriseConnection enterpriseConnection;
    
    private UpdateAccount(){
        setEnterpriseConnection(ConnectionHelper.doConnect());
    }
    
    public static void main(String[] args){
        UpdateAccount updateAccount = new UpdateAccount();
        //Here you need to provide Account ID here
        updateAccount.doUpdateAccount(new String[]{"0019000001ByhBI","0019000000tvEQo"});
    }
    
    private void doUpdateAccount(String[] ids){
        Account[] toBeUpdatedAccount = new Account[ids.length];
        
        for(int i = 0 ; i < ids.length ; i++){
            Account tempAccount = new Account();
            tempAccount.setId(ids[i]);
            tempAccount.setRating("Hot");
            toBeUpdatedAccount[i] = tempAccount;
        }
        
        //Update the account
        try{
            SaveResult[] results = getEnterpriseConnection().update(toBeUpdatedAccount);
            for(SaveResult eachResult : results){
                if(eachResult.isSuccess()){
                    System.out.println("Account updated with ID: " + eachResult.getId());
                }else{
                    for(Error eachError : eachResult.getErrors()){
                        System.out.println("Error message is: " + eachError.getMessage());
                    }
                }
            }
        }catch(ConnectionException e){
            e.printStackTrace();
        }
    }

    public EnterpriseConnection getEnterpriseConnection() {
        return enterpriseConnection;
    }

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


Now in the below post, I would like to add an existing account to an existing opportunity. I can do that with update() method by passing the opportunity ID and the External ID value of the account record. Important: Keep in mind you need to pass the External ID value of the account record. In my Org, I have setup a custom field Account_External_ID__c and marked that as External ID.
Below code will help you to understand how you can achieve the above requirement -
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Opportunity;
import com.sforce.soap.enterprise.sobject.SObject;
import com.sforce.ws.ConnectionException;


public class UpdateOpportunityAccount {
    private EnterpriseConnection enterpriseConnection;
    
    private UpdateOpportunityAccount(){
        setEnterpriseConnection(ConnectionHelper.doConnect());
    }
    
    public static void main(String[] args) {
        UpdateOpportunityAccount updateOpportunityAccount = new UpdateOpportunityAccount();
        updateOpportunityAccount.doUpdateOpportunityAccount("0069000000ReDgi","EXID1111");
    }
    
    private void doUpdateOpportunityAccount(String opportunityId, String accountExternalId){
        try{
            //Link an existing opportunity
            Opportunity toBeUpdatedOpportunity = new Opportunity();
            toBeUpdatedOpportunity.setId(opportunityId);
            
            //Link an existing account
            Account toBeLinkedAccount = new Account();
            toBeLinkedAccount.setAccount_External_ID__c(accountExternalId);
            
            //Link Opportunity with Account
            toBeUpdatedOpportunity.setAccount(toBeLinkedAccount);
            
            SaveResult[] results = getEnterpriseConnection().update(new SObject[]{toBeUpdatedOpportunity});
            for(SaveResult eachResult : results){
                if(eachResult.isSuccess()){
                    System.out.println("Created succesfully with ID: " + eachResult.getId());
                }else{
                    for(Error eachError : eachResult.getErrors()){
                        System.out.println("Error message is: " + eachError.getMessage());
                    }
                }
            }
        }catch(ConnectionException e){
            e.printStackTrace();
        }
    }
    
    public EnterpriseConnection getEnterpriseConnection() {
        return enterpriseConnection;
    }
    
    public void setEnterpriseConnection(EnterpriseConnection enterpriseConnection) {
        this.enterpriseConnection = enterpriseConnection;
    }

}


Appreciate your feedback. Thanks.

In my next post, I will explain the upsert operation. Till then keep learning Salesforce.

0 comments:

Post a Comment