Month: February 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 @ Salesforce Integration with SOAP API – Part 4 – Java program for connecting to Salesforce using SOAP API and Enterprise WSDLBelow is the Java program – UpsertOperation.javaimport 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...

Read More

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 APINote: 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 @ Salesforce Integration with SOAP API – Part 4 – Java program for connecting to Salesforce using SOAP API and Enterprise WSDLNow 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...

Read More

Salesforce Integration with SOAP API – Part 7 – Create Account records with Java program

Preface – This post is part of “Salesforce Integration with SOAP API” series.As of now we have understood how we can query one or more record from Salesforce using query() and queryMore() methods. The posts are @Salesforce Integration with SOAP API – Part 5 – Query Account Record with Java programSalesforce Integration with SOAP API – Part 6 – Bulkify your code – Query more Account records with Java programBut what about creating records – Create() method is used to create one or more records in the Salesforce Org. This is basically doing the INSERT operation. The basic steps are given below –Create an array of sObject[] where each sObject is basically the record you want to create. You need to populate all the required fields.Call create(), passing the sObject[] arrayProcess the results in the SaveResult[] object.We should understand the below important points before going into the coding –API generates the unique ID fields automatically. As a developer, we can’t explicitly set the value of IDThere are few other fields also getting generated automatically and we can’t set them explicitly. The fields are CreatedDate, CreatedById, LastModifiedDate, LastModifiedById and SystemModstamp.For all required fields, if there is no default values, as a developer you need to provide the values.For all non-required fields, if you don’t provide any values, API will put null.As a developer, you need to respect the referential integrity....

Read More

Salesforce Integration with SOAP API – Part 6 – Bulkify your code – Query more Account records with Java program

Preface – This post is part of “Salesforce Integration with SOAP API” series.In my previous post, I have explained how to query Account record. The post is @Salesforce Integration with SOAP API – Part 5 – Query Account Record with Java programBut say for example, if you have huge amount of record you need to fetch from Salesforce using SOAP API, then what you should do? Basically how you can bulkify your code?Below is what Salesforce documented -Please have a look what Salesforce tells about QueryLocator. Before going to the code, you need to understand the importance of QueryLocator.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 @ Salesforce Integration with SOAP API – Part 4 – Java program for connecting to Salesforce using SOAP API and Enterprise WSDLNow below is the Java code -import com.sforce.soap.enterprise.EnterpriseConnection;import com.sforce.soap.enterprise.QueryResult;import com.sforce.soap.enterprise.sobject.Account;import com.sforce.soap.enterprise.sobject.SObject;import com.sforce.ws.ConnectionException;public class QueryAllAccounts { private EnterpriseConnection connection; private QueryAllAccounts() { setConnection(ConnectionHelper.doConnect()); } public static void main(String[] args) { QueryAllAccounts queryAllAccounts = new QueryAllAccounts(); queryAllAccounts.doQuery(); } private void doQuery(){ QueryResult queryResult = new QueryResult(); try{ queryResult = getConnection().query(“SELECT Id,Name,Phone,Rating FROM Account”); boolean done = false; if(queryResult.getSize() > 0){ System.out.println(“Found ” + queryResult.getSize() + ” records.”); while(!done){ for(SObject sObject : queryResult.getRecords()){ Account anAccount = (Account)sObject; System.out.println(“Account ID: ” + anAccount.getId() + ” Account Name: ” + anAccount.getName() +...

Read More

Salesforce Integration with SOAP API – Part 5 – Query Account Record with Java program

Preface – This post is part of “Salesforce Integration with SOAP API” series.In my previous post, I have explained how can you configure your Eclipse IDE so that you can start writing you own Java program for connecting to Salesforce using SOAP API and also the Java program to check the connectivity. If you are not familiar, I would request you to go through my previous posts. The links are given below -Salesforce Integration with SOAP API – Part 3 – Configure Eclipse for Enterprise WSDLSalesforce Integration with SOAP API – Part 4 – Java program for connecting to Salesforce using SOAP API and Enterprise WSDLFrom this post, I will try to become more technical and concentrate on CRUD (Create, Read, Update and Delete) operation using Java Program and Salesforce’s SOAP API methods.This is the first post where I will explain how you can query Salesforce records using Java program and SOAP API. The below program will fetch a specific Account record and display the information in console -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 @ Salesforce Integration with SOAP API – Part 4 – Java program for connecting to Salesforce using SOAP API and Enterprise WSDLBelow is the java program which will fetch details of the account named “Burlington Textiles...

Read More

Salesforce Integration with SOAP API – Part 4 – Java program for connecting to Salesforce using SOAP API and Enterprise WSDL

Preface – This post is part of “Salesforce Integration with SOAP API” series.In my last post, I have explained how you can configure your Eclipse so that you can start writing your first Java Program to connect to Salesforce using SOAP API. Below is the link of that post -Salesforce Integration with SOAP API – Part 3 – Configure Eclipse for Enterprise WSDLHere comes the Java Helper Class to connect to Salesforce using SOAP API.ConnectionHelper.java:import com.sforce.soap.enterprise.Connector;import com.sforce.soap.enterprise.EnterpriseConnection;import com.sforce.ws.ConnectionException;import com.sforce.ws.ConnectorConfig;public class ConnectionHelper { static final String USERNAME = “<SALESFORCE USERNAME>”; static final String PASSWORD = “<SALESFORCE PASSWORD><SECURITY TOKEN>”; public static EnterpriseConnection doConnect(){ EnterpriseConnection connection = null; ConnectorConfig config = new ConnectorConfig(); config.setUsername(USERNAME); config.setPassword(PASSWORD); //Add proxy details if requoired config.setProxy(“<PROXY URL>”,<PROXY PORT>); config.setProxyPassword(“<PROXY PASSWORD>”); try{ connection = Connector.newConnection(config); System.out.println(“Auth EndPoint: “+config.getAuthEndpoint()); System.out.println(“Service EndPoint: “+config.getServiceEndpoint()); System.out.println(“Username: “+config.getUsername()); System.out.println(“SessionId: “+config.getSessionId()); }catch (ConnectionException e1) { e1.printStackTrace(); } return connection; }}Now I will write the main method to call the doConnect method for connecting to Salesforce. The code is given below -CheckConnectivity.javapublic class CheckConnectivity { public static void main(String[] args) { ConnectionHelper.doConnect(); }}Output -You should see an output like this below -Auth EndPoint: https://login.salesforce.com/services/Soap/c/32.0/0DF900000004uQhService EndPoint: https://ap1.salesforce.com/services/Soap/c/32.0/00D90000000s4Hg/0DF900000004uQhUsername: <YOUR USER NAME>SessionId: 00D90000000s4Hg!ARkAQNCqDG9j80Xm52KDgbN0aZSuQLP0Ql1q0XedDIZ8jymlHtG3Y2KvIEJ04A0KJqPBgqhVzcNvQZ.Wy4k7ugk_Wt0RKACxThat’s all. From my next posts, I will start explaining all the CRUD operations using SOAP API. .Till then any feedback, please let me know....

Read More

Archives