Newer Version Available
Batch Resource
Use this resource to minimize the number of round-trips between the client and the server. For example, in a mobile app, when a user taps the button to like a feed item, the response body includes information about the like, but it doesn’t include the total number of likes on the feed item. To like the feed item, POST a request to /chatter/feed-elements/feedElementId/capabilities/chatter-likes/items. To get the like total, GET the feed item information from /chatter/feed-elements/feedElementId/capabilities/chatter-likes/items.
Add both requests to a Generic Batch Collection Input request body and send them both in a single POST request to /connect/batch:
1{
2 "batchRequests" : [
3 {
4 "method" : "Post",
5 "url" : "/v53.0/chatter/feed-elements/feedElementId/capabilities/chatter-likes/items"
6 },
7 {
8 "method" : "Get",
9 "url" : "/v53.0/chatter/feed-elements/feedElementId/capabilities/chatter-likes/items"
10 }
11 ],
12 "haltOnError" : "false"
13}The requests in a batch are called subrequests. All subrequests are executed in the context of the same user. Subrequests are independent and information can���t be passed between them. Subrequests execute serially in their order in the request body. When a subrequest executes successfully, it commits its data. Commits are reflected in the output of later subrequests (as in the previous example which liked a feed item then got the total number of likes). If a subrequest fails, commits made by previous subrequests are not rolled back.
- Resource
-
1/connect/batch - Available since version
- 28.0
- Requires Chatter
- No
- HTTP methods
- POST
- Request body
-
Generic Batch Collection Input
- Root XML Tag
- <batch>
- JSON
-
1{ 2 "batchRequests" : [ 3 { 4 "method" : "Get", 5 "url" : "/v53.0/chatter/feeds/news/me" 6 }, 7 { 8 "method" : "Get", 9 "url" : "/v53.0/chatter/feeds/user-profile/me" 10 } 11 ] 12} - Properties
-
Name Type Description Available Version batchRequests Generic Batch Input[] Collection of Batch Request Input request bodies containing the subrequest URLs to execute. 28.0 haltOnError Boolean Controls whether Salesforce stops processing subrequests if a subrequest fails. The default value is false. If the value is false and a subrequest in the batch does not complete, Salesforce attempts to execute subsequent subrequests in the batch.
If the value is true and a subrequest in the batch doesn’t complete due to an HTTP response in the 400 or 500 range, Salesforce halts execution. It doesn’t attempt to execute subsequent subrequests in the batch. It returns an HTTP 412 status code and a BATCH_PROCESSING_HALTED error message for each subsequent subrequest indicating that a previous request was unsuccessful. The top-level request to /connect/batch returns HTTP 200, and the hasErrors property in the response is set to true.
28.0
- Request parameters
- None.
- Response
- Batch Results
- Example
- This example is a POST request made to https://instance_name/services/data/v53.0/connect/batch that includes the following request body
containing two
subrequests:
1{ 2 "batchRequests" : [ 3 { 4 "method" : "Get", 5 "url" : "/v53.0/chatter/feeds/user-profile/me" 6 }, 7 { 8 "method" : "Get", 9 "url" : "/v53.0/chatter/feeds/news/me" 10 } 11 ] 12}The Batch Results response body includes a Batch Result Item response body for each subrequest:1{ 2 "hasErrors": false, 3 "results": [ 4 { 5 "result": { 6 "feedElementPostUrl": "/services/data/v53.0/chatter/feed-elements", 7 "feedElementsUrl": "/services/data/v53.0/chatter/feeds 8/user-profile/005D0000001LL8OIAW/feed-elements", 9 "isModifiedUrl": null 10 }, 11 "statusCode": 200 12 }, 13 { 14 "result": { 15 "feedElementPostUrl": "/services/data/v53.0/chatter/feed-elements", 16 "feedElementsUrl": "/services/data/v53.0/chatter/feeds 17/news/005D0000001LL8OIAW/feed-elements", 18 "isModifiedUrl": null 19 }, 20 "statusCode": 200 21 } 22 ] 23} - Example: haltOnError is true
- This example is the same as the previous example but it includes an error in
the first subrequest and haltOnError is
set to true. Note that the first
subrequest contains a typo.
1{ 2 "batchRequests" : [ 3 { 4 "method" : "Get", 5 "url" : "/v53.0/chatter/feeds/user-profile/men" 6 }, 7 { 8 "method" : "Get", 9 "url" : "/v53.0/chatter/feeds/news/me" 10 } 11 ], 12 "haltOnError" : "true" 13}The Batch Results response body includes a Batch Result Item response body for each subrequest. The top-level request to /connect/batch returns an HTTP 200, but the first subrequest returns an HTTP 404 because the resource can’t be found. Because haltOnError is true, the next subrequest returns an HTTP 412. Also note that hasErrors is true.1{ 2 "hasErrors": true, 3 "results": [ 4 { 5 "result": [{ 6 "errorCode": "NOT_FOUND", 7 "message": "The requested resource does not exist" 8 }], 9 "statusCode": 404 10 }, 11 { 12 "result": [{ 13 "errorCode": "BATCH_PROCESSING_HALTED", 14 "message": "Batch processing halted per request" 15 }], 16 "statusCode": 412 17 } 18 ] 19}
Post multiple comments with binary files
Include the request body of each subrequest in the richInput properties.
Also include information about the binary parts in the binaryPartName and binaryPartNameAlias properties.
1curl -H "X-PrettyPrint: 1" -F 'json={ "haltOnError":true, "batchRequests":[ { "url":"/v33.0/chatter/feed-elements/0D5D0000000YG0N/capabilities/comments/items",
2"method":"Post", "binaryPartName":"binaryPart1", "binaryPartNameAlias":"feedElementFileUpload", "richInput":{ "body":{ "messageSegments":[ { "type":"Text", "text":"Check out this file, it may help." } ] },
3 "capabilities":{ "content":{ "title":"Presentation1.txt" } } } }, { "url":"/v33.0/chatter/feed-elements/0D5D0000000YG0M/capabilities/comments/items",
4"method":"Post", "binaryPartName":"binaryPart2", "binaryPartNameAlias":"feedElementFileUpload", "richInput":{ "body":{ "messageSegments":[ { "type":"Text", "text":"Check out this file, it may help." } ] },
5 "capabilities":{ "content":{ "title":"Presentation2.txt" } } } } ] };type=application/json' -F "binaryPart1=@/Users/jbleyle/Desktop/Presentation1.txt;type=application/octet-stream"
6-F "binaryPart2=@/Users/jbleyle/Desktop/Presentation2.txt;type=application/octet-stream"
7-X POST https://instance_name/services/data/v53.0/connect/batch
8-H 'Authorization: OAuth 00DD00000007HoR!...' --insecure1https://instance_name/services/data/v53.0/connect/batch
2
3Authorization: OAuth 00DD0000000Jhd2!...
4Accept: application/json
5Host: instance_name
6Content-Type: multipart/form-data; boundary=123123
7
8--123123
9Content-Disposition: form-data; name="json"
10Content-Type: application/json
11
12{
13 "haltOnError":true,
14 "batchRequests":[
15 {
16 "url":"/v53.0/chatter/feed-elements/0D5D0000000YG0D/capabilities/comments/items",
17 "method":"Post",
18 "binaryPartName":"binaryPart1",
19 "binaryPartNameAlias":"feedElementFileUpload",
20 "richInput":{
21 "body":{
22 "messageSegments":[
23 {
24 "type":"Text",
25 "text":"Check out this file, it may help."
26 }
27 ]
28 },
29 "capabilities":{
30 "content":{
31 "title":"Presentation1.txt"
32 }
33 }
34 }
35 },
36 {
37 "url":"/v53.0/chatter/feed-elements/0D5D0000000YG0H/capabilities/comments/items",
38 "method":"Post",
39 "binaryPartName":"binaryPart2",
40 "binaryPartNameAlias":"feedElementFileUpload",
41 "richInput":{
42 "body":{
43 "messageSegments":[
44 {
45 "type":"Text",
46 "text":"Check out this file, it may help."
47 }
48 ]
49 },
50 "capabilities":{
51 "content":{
52 "title":"Presentation2.txt"
53 }
54 }
55 }
56 }
57 ]
58}
59
60
61--123123
62Content-Disposition: form-data; name="binaryPart1"; filename="Presentation1.txt"
63Content-Type: application/octet-stream; charset=ISO-8859-1
64Content-Transfer-Encoding: binary
65
66This is the content of file 1
67--123123
68Content-Disposition: form-data; name="binaryPart2"; filename="Presentation2.txt"
69Content-Type: application/octet-stream; charset=ISO-8859-1
70Content-Transfer-Encoding: binary
71
72This is the content of file 2
73--123123--