Step-By-Step Guide to Get Started with Salesforce REST API using Java

http://www.asagarwal.com/2401/step-by-step-guide-to-get-started-with-salesforce-rest-api-using-java

How do you connect to Salesforce from an external application, say, for example Java? If you Google for the information, you will quickly learn that Salesforce provides different methods and different APIs. But what you will probably not find is a step-by-step guide teaching you exactly how to do it. In this blog post, I have stitched together a complete step-by-step instructions with screenshots on how to download, install, configure and use Salesforce’s REST API to connect from a java program and execute some basic transactions (query, insert, update, delete records). In less than 45 minutes, you will have a fully functional java program that will connect to Salesforce and execute transactions in Salesforce.

The guide includes downloading & installing necessary HttpClient and JSON frameworks that is required to work with Salesforce REST APIs. This is followed by referencing the JAR file in your java program, logging on to Salesforce from the java program, querying and manipulating data. Here is an overview of steps that needs to be completed and is covered in the guide

  • Install Java
  • Install Eclipse
  • Install HttpClient
  • Install JSON Framework
  • Create Connected Apps in Salesforce
  • Setup REST Environment in Java
  • Create Java Program
  • Execute Transactions using REST API

(Once you are done with this, you may also want to explore my other blog post Step-By-Step Guide to Get Started with Salesforce SOAP API using Java to learn how to get started with Salesforce SOAP API using Java )

Even if you are not a developer, you should be able to follow the guide and have working java program at the end of it.  Sounds interesting? Download the following presentation and let’s get started…

?
Java Code to Connect to Salesforce using REST API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package salesforce_rest;
 
import java.io.IOException;
 
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;
 
public class Main {
 
     static final String USERNAME     = "username@salesforce.com" ;
     static final String PASSWORD     = "passwordSecurityToken" ;
     static final String LOGINURL     = "https://login.salesforce.com" ;
     static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password" ;
     static final String CLIENTID     = "ConsumerKeyFromSalesfoceConnectedApps" ;
     static final String CLIENTSECRET = "ConsumerSecretFromSalesforceConnectedApps" ;
 
     public static void main(String[] args) {
 
         HttpClient httpclient = HttpClientBuilder.create().build();
 
         // Assemble the login request URL
         String loginURL = LOGINURL +
                           GRANTSERVICE +
                           "&client_id=" + CLIENTID +
                           "&client_secret=" + CLIENTSECRET +
                           "&username=" + USERNAME +
                           "&password=" + PASSWORD;
 
         // Login requests must be POSTs
         HttpPost httpPost = new HttpPost(loginURL);
         HttpResponse response = null ;
 
         try {
             // Execute the login POST request
             response = httpclient.execute(httpPost);
         } catch (ClientProtocolException cpException) {
             cpException.printStackTrace();
         } catch (IOException ioException) {
             ioException.printStackTrace();
         }
 
         // verify response is HTTP OK
         final int statusCode = response.getStatusLine().getStatusCode();
         if (statusCode != HttpStatus.SC_OK) {
             System.out.println( "Error authenticating to Force.com: " +statusCode);
             // Error is in EntityUtils.toString(response.getEntity())
             return ;
         }
 
         String getResult = null ;
         try {
             getResult = EntityUtils.toString(response.getEntity());
         } catch (IOException ioException) {
             ioException.printStackTrace();
         }
         JSONObject jsonObject = null ;
         String loginAccessToken = null ;
         String loginInstanceUrl = null ;
         try {
             jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
             loginAccessToken = jsonObject.getString( "access_token" );
             loginInstanceUrl = jsonObject.getString( "instance_url" );
         } catch (JSONException jsonException) {
             jsonException.printStackTrace();
         }
         System.out.println(response.getStatusLine());
         System.out.println( "Successful login" );
         System.out.println( "  instance URL: " +loginInstanceUrl);
         System.out.println( "  access token/session ID: " +loginAccessToken);
 
         // release connection
         httpPost.releaseConnection();
     }
}
?
Java Code to Connect, Query, Insert, Update & Delete to Salesforce using REST API
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package salesforce_rest;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
 
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONTokener;
import org.json.JSONException;
 
public class Main {
 
     static final String USERNAME     = "username@salesforce.com" ;
     static final String PASSWORD     = "passwordSecurityToken" ;
     static final String LOGINURL     = "https://login.salesforce.com" ;
     static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password" ;
     static final String CLIENTID     = "ConsumerKeyFromSalesfoceConnectedApps" ;
     static final String CLIENTSECRET = "ConsumerSecretFromSalesforceConnectedApps" ;
     private static String REST_ENDPOINT = "/services/data" ;
     private static String API_VERSION = "/v32.0" ;
     private static String baseUri;
     private static Header oauthHeader;
     private static Header prettyPrintHeader = new BasicHeader( "X-PrettyPrint" , "1" );
     private static String leadId ;
     private static String leadFirstName;
     private static String leadLastName;
     private static String leadCompany;
 
     public static void main(String[] args) {
 
         HttpClient httpclient = HttpClientBuilder.create().build();
 
         // Assemble the login request URL
         String loginURL = LOGINURL +
                           GRANTSERVICE +
                           "&client_id=" + CLIENTID +
                           "&client_secret=" + CLIENTSECRET +
                           "&username=" + USERNAME +
                           "&password=" + PASSWORD;
 
         // Login requests must be POSTs
         HttpPost httpPost = new HttpPost(loginURL);
         HttpResponse response = null ;
 
         try {
             // Execute the login POST request
             response = httpclient.execute(httpPost);
         } catch (ClientProtocolException cpException) {
             cpException.printStackTrace();
         } catch (IOException ioException) {
             ioException.printStackTrace();
         }
 
         // verify response is HTTP OK
         final int statusCode = response.getStatusLine().getStatusCode();
         if (statusCode != HttpStatus.SC_OK) {
             System.out.println( "Error authenticating to Force.com: " +statusCode);
             // Error is in EntityUtils.toString(response.getEntity())
             return ;
         }
 
         String getResult = null ;
         try {
             getResult = EntityUtils.toString(response.getEntity());
         } catch (IOException ioException) {
             ioException.printStackTrace();
         }
 
         JSONObject jsonObject = null ;
         String loginAccessToken = null ;
         String loginInstanceUrl = null ;
 
         try {
             jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
             loginAccessToken = jsonObject.getString( "access_token" );
             loginInstanceUrl = jsonObject.getString( "instance_url" );
         } catch (JSONException jsonException) {
             jsonException.printStackTrace();
         }
 
         baseUri = loginInstanceUrl + REST_ENDPOINT + API_VERSION ;
         oauthHeader = new BasicHeader( "Authorization" , "OAuth " + loginAccessToken) ;
         System.out.println( "oauthHeader1: " + oauthHeader);
         System.out.println( "\n" + response.getStatusLine());
         System.out.println( "Successful login" );
         System.out.println( "instance URL: " +loginInstanceUrl);
         System.out.println( "access token/session ID: " +loginAccessToken);
         System.out.println( "baseUri: " + baseUri);       
 
         // Run codes to query, isnert, update and delete records in Salesforce using REST API
         queryLeads();
         createLeads();
         updateLeads();
         deleteLeads();       
 
         // release connection
         httpPost.releaseConnection();
     }
 
     // Query Leads using REST HttpGet
     public static void queryLeads() {
         System.out.println( "\n_______________ Lead QUERY _______________" );
         try {
 
             //Set up the HTTP objects needed to make the request.
             HttpClient httpClient = HttpClientBuilder.create().build();
 
             String uri = baseUri + "/query?q=Select+Id+,+FirstName+,+LastName+,+Company+From+Lead+Limit+5" ;
             System.out.println( "Query URL: " + uri);
             HttpGet httpGet = new HttpGet(uri);
             System.out.println( "oauthHeader2: " + oauthHeader);
             httpGet.addHeader(oauthHeader);
             httpGet.addHeader(prettyPrintHeader);
 
             // Make the request.
             HttpResponse response = httpClient.execute(httpGet);
 
             // Process the result
             int statusCode = response.getStatusLine().getStatusCode();
             if (statusCode == 200 ) {
                 String response_string = EntityUtils.toString(response.getEntity());
                 try {
                     JSONObject json = new JSONObject(response_string);
                     System.out.println( "JSON result of Query:\n" + json.toString( 1 ));
                     JSONArray j = json.getJSONArray( "records" );
                     for ( int i = 0 ; i < j.length(); i++){
                         leadId = json.getJSONArray( "records" ).getJSONObject(i).getString( "Id" );
                         leadFirstName = json.getJSONArray( "records" ).getJSONObject(i).getString( "FirstName" );
                         leadLastName = json.getJSONArray( "records" ).getJSONObject(i).getString( "LastName" );
                         leadCompany = json.getJSONArray( "records" ).getJSONObject(i).getString( "Company" );
                         System.out.println( "Lead record is: " + i + ". " + leadId + " " + leadFirstName + " " + leadLastName + "(" + leadCompany + ")" );
                     }
                 } catch (JSONException je) {
                     je.printStackTrace();
                 }
             } else {
                 System.out.println( "Query was unsuccessful. Status code returned is " + statusCode);
                 System.out.println( "An error has occured. Http status: " + response.getStatusLine().getStatusCode());
                 System.out.println(getBody(response.getEntity().getContent()));
                 System.exit(- 1 );
             }
         } catch (IOException ioe) {
             ioe.printStackTrace();
         } catch (NullPointerException npe) {
             npe.printStackTrace();
         }
     }
 
     // Create Leads using REST HttpPost
     public static void createLeads() {
         System.out.println( "\n_______________ Lead INSERT _______________" );
 
         String uri = baseUri + "/sobjects/Lead/" ;
         try {
 
             //create the JSON object containing the new lead details.
             JSONObject lead = new JSONObject();
             lead.put( "FirstName" , "REST API" );
             lead.put( "LastName" , "Lead" );
             lead.put( "Company" , "asagarwal.com" );
 
             System.out.println( "JSON for lead record to be inserted:\n" + lead.toString( 1 ));
 
             //Construct the objects needed for the request
             HttpClient httpClient = HttpClientBuilder.create().build();
 
             HttpPost httpPost = new HttpPost(uri);
             httpPost.addHeader(oauthHeader);
             httpPost.addHeader(prettyPrintHeader);
             // The message we are going to post
             StringEntity body = new StringEntity(lead.toString( 1 ));
             body.setContentType( "application/json" );
             httpPost.setEntity(body);
 
             //Make the request
             HttpResponse response = httpClient.execute(httpPost);
 
             //Process the results
             int statusCode = response.getStatusLine().getStatusCode();
             if (statusCode == 201 ) {
                 String response_string = EntityUtils.toString(response.getEntity());
                 JSONObject json = new JSONObject(response_string);
                 // Store the retrieved lead id to use when we update the lead.
                 leadId = json.getString( "id" );
                 System.out.println( "New Lead id from response: " + leadId);
             } else {
                 System.out.println( "Insertion unsuccessful. Status code returned is " + statusCode);
             }
         } catch (JSONException e) {
             System.out.println( "Issue creating JSON or processing results" );
             e.printStackTrace();
         } catch (IOException ioe) {
             ioe.printStackTrace();
         } catch (NullPointerException npe) {
             npe.printStackTrace();
         }
     }
 
     // Update Leads using REST HttpPatch. We have to create the HTTPPatch, as it does not exist in the standard library
     // Since the PATCH method was only recently standardized and is not yet implemented in Apache HttpClient
     public static void updateLeads() {
         System.out.println( "\n_______________ Lead UPDATE _______________" );
 
         //Notice, the id for the record to update is part of the URI, not part of the JSON
         String uri = baseUri + "/sobjects/Lead/" + leadId;
         try {
             //Create the JSON object containing the updated lead last name
             //and the id of the lead we are updating.
             JSONObject lead = new JSONObject();
             lead.put( "LastName" , "Lead --UPDATED" );
             System.out.println( "JSON for update of lead record:\n" + lead.toString( 1 ));
 
             //Set up the objects necessary to make the request.
             //DefaultHttpClient httpClient = new DefaultHttpClient();
             HttpClient httpClient = HttpClientBuilder.create().build();
 
             HttpPatch httpPatch = new HttpPatch(uri);
             httpPatch.addHeader(oauthHeader);
             httpPatch.addHeader(prettyPrintHeader);
             StringEntity body = new StringEntity(lead.toString( 1 ));
             body.setContentType( "application/json" );
             httpPatch.setEntity(body);
 
             //Make the request
             HttpResponse response = httpClient.execute(httpPatch);
 
             //Process the response
             int statusCode = response.getStatusLine().getStatusCode();
             if (statusCode == 204 ) {
                 System.out.println( "Updated the lead successfully." );
             } else {
                 System.out.println( "Lead update NOT successfully. Status code is " + statusCode);
             }
         } catch (JSONException e) {
             System.out.println( "Issue creating JSON or processing results" );
             e.printStackTrace();
         } catch (IOException ioe) {
             ioe.printStackTrace();
         } catch (NullPointerException npe) {
             npe.printStackTrace();
         }
     }
 
     // Extend the Apache HttpPost method to implement an HttpPatch
     private static class HttpPatch extends HttpPost {
         public HttpPatch(String uri) {
             super (uri);
         }
 
         public String getMethod() {
             return "PATCH" ;
         }
     }
 
     // Update Leads using REST HttpDelete (We have to create the HTTPDelete, as it does not exist in the standard library.)
     public static void deleteLeads() {
         System.out.println( "\n_______________ Lead DELETE _______________" );
 
         //Notice, the id for the record to update is part of the URI, not part of the JSON
         String uri = baseUri + "/sobjects/Lead/" + leadId;
         try {
             //Set up the objects necessary to make the request.
             HttpClient httpClient = HttpClientBuilder.create().build();
 
             HttpDelete httpDelete = new HttpDelete(uri);
             httpDelete.addHeader(oauthHeader);
             httpDelete.addHeader(prettyPrintHeader);
 
             //Make the request
             HttpResponse response = httpClient.execute(httpDelete);
 
             //Process the response
             int statusCode = response.getStatusLine().getStatusCode();
             if (statusCode == 204 ) {
                 System.out.println( "Deleted the lead successfully." );
             } else {
                 System.out.println( "Lead delete NOT successful. Status code is " + statusCode);
             }
         } catch (JSONException e) {
             System.out.println( "Issue creating JSON or processing results" );
             e.printStackTrace();
         } catch (IOException ioe) {
             ioe.printStackTrace();
         } catch (NullPointerException npe) {
             npe.printStackTrace();
         }
     }
 
     private static String getBody(InputStream inputStream) {
         String result = "" ;
         try {
             BufferedReader in = new BufferedReader(
                     new InputStreamReader(inputStream)
             );
             String inputLine;
             while ( (inputLine = in.readLine() ) != null ) {
                 result += inputLine;
                 result += "\n" ;
             }
             in.close();
         } catch (IOException ioe) {
             ioe.printStackTrace();
         }
         return result;
     }
}

Not only this, you can go a step further and use other RESTful APIs that Salesforce provides to extract and manipulate data and metadata. For a list of different APIs along with a brief explanation on which API should be used when, please navigate to URL https://help.salesforce.com/HTViewHelpDoc?id=integrate_what_is_api.htm

Other References:

  1. Salesforce REST API Developer Guide – https://www.salesforce.com/us/developer/docs/api_rest/
  2. Setting Up Your Java Developer Environment –https://help.salesforce.com/help/pdfs/en/salesforce_developer_environment_tipsheet.pdf

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值