4

The REST API docs on update records seem to indicate that it is possible to update multiple records with a PATCH call, but does not provide a concrete examples of how to do so. The doc says:

Provide the updated record information in your request data and use the PATCH method of the resource with a specific record ID to update that record. Records in a single file must be of the same object type.

Searching google and SFSE I see references to using the composite resource for bulk operations, with a concrete example here.

So my question, in two parts

  • is it possible to update multiple records using a patch call?

  • if so, can i serialize a list of sObjects using JSON.serialize or do i need to build my own JSON?

  • if not, can I serialize a list of sObjects and pass them to the composite resource?

So far I've been unable to do anything (trying to call salesforce api from apex to update currencyType records), and not sure if i'm dealing with the POST/PATCH issues or if im just not hitting the right endpoint or have the wrong file structure.

debug log shows:

14:24:17:349 CALLOUT_REQUEST [111]|System.HttpRequest[Endpoint=https://cs13.salesforce.com/services/data/v41.0/sobjects/CurrencyType?_HttpMethod=PATCH, Method=POST]

14:24:17:394 CALLOUT_RESPONSE [111]|System.HttpResponse[Status=Method Not Allowed, StatusCode=405]

UPDATE : here is a snippet of the request body. I'm serializing a list of CurrencyType sObjects

[{"attributes":{"type":"CurrencyType","url":"/services/data/v41.0/sobjects/CurrencyType/01L400000002ASiEAM"},"Id":"01L400000002ASiEAM","IsoCode":"AED","ConversionRate":3.672611,"IsActive":true},{"attributes":{"type":"CurrencyType","url":"/services/data/v41.0/sobjects/CurrencyType/01L40000000V05bEAC"},"Id":"01L40000000V05bEAC","IsoCode":"AFN","ConversionRate":68.671,"IsActive":true},{"attributes":{"type":"CurrencyType","url":"/services/data/v41.0/sobjects/CurrencyType/01L400000002ABhEAM"},"Id":"01L400000002ABhEAM","IsoCode":"ARS","ConversionRate":17.5205,"IsActive":true},{"attributes":{"type":"CurrencyType","url":"/services/data/v41.0/sobjects/CurrencyType/01L40000000V08pEAC"},"Id":"01L40000000V08pEAC","IsoCode":"AUD","ConversionRate":1.27665,"IsActive":true},{"attributes":{"type":"CurrencyType","url":"/services/data/v41.0/sobjects/CurrencyType/01L400000002ADJEA2"},"Id":"01L400000002ADJEA2","IsoCode":"BDT","ConversionRate":82.808961,"IsActive":true},{"attributes":{"type":"CurrencyType","url":"/services/data/v41.0/sobjects/CurrencyType/01L40000000ZCJiEAO"},"Id":"01L40000000ZCJiEAO","IsoCode":"BHD","ConversionRate":0.379507,"IsActive":true},{"attributes":{"type":"CurrencyType","url":"/services/data/v41.0/sobjects/CurrencyType/01L400000002ABmEAM"},"Id":"01L400000002ABmEAM","IsoCode":"BOB","ConversionRate":7.007198,"IsActive":true}

4

You will need to create your own JSON for the REST API Composite batch rather than directly using the source sObjects.

Here is an example of updating a collection of OpportunityLineItem records.

POST /services/data/v38.0/composite/batch HTTP/1.1
Host: na5.salesforce.com
Authorization: Bearer 00D700000000001!AQ0AQOzUlrjD_StillNotARealSession_x61fsbSSJ123456789mKjmhS0myiYYK
Content-Type: application/json

{
 "batchRequests": [{
   "method": "PATCH",
   "url": "v38.0/sobjects/OpportunityLineItem/00k7000000eaaZBAAY",
   "richInput": {
    "End_Date__c": "2017-01-19"
   }
  }, {
   "method": "PATCH",
   "url": "v38.0/sobjects/OpportunityLineItem/00k7000000eaaZCAAY",
   "richInput": {
    "End_Date__c": "2017-01-19"
   }
  }, {
   "method": "PATCH",
   "url": "v38.0/sobjects/OpportunityLineItem/00k7000000eaaZDAAY",
   "richInput": {
    "End_Date__c": "2017-01-19"
   }
  }, {
   "method": "PATCH",
   "url": "v38.0/sobjects/OpportunityLineItem/00k7000000eaaZEAAY",
   "richInput": {
    "End_Date__c": "2017-01-19"
   }
  }, {
   "method": "PATCH",
   "url": "v38.0/sobjects/OpportunityLineItem/00k7000000eaaZFAAY",
   "richInput": {
    "End_Date__c": "2017-01-19"
   }
  },
                //...

 ]
}

Note that this was a POST rather than PATCH request.

Generally:

  1. Make sure you are sending a POST to /services/data/v41.0/composite/batch
  2. The JSON should start with the batchRequests array.
  3. Each individual batch request should specify its method as PATCH
  4. Each individual batch request should specify its url. This should be the URL for updating the sObject and include its ID
  5. Finally, specify just the fields you want to change.

I've written about this previously in Choose Your Own Adventure - Dirty Dozen showdown with the REST API vs SOAP API vs BULK API. If you can, using the Bulk API will give you good performance at the cost of more API calls to get it going.

  • thanks Daniel. I was including the _HttpMethod=PATCH url parameter but seems unsuccessful, but looks like that doesnt matter as i need to use the composite endpoint. Looks like i need to get to know the JSON generator. This is a side project w a small number of records, like 40, updating 1x a day so ease of building is more impt for me than performance. ill check the link out. thanks again. – gorav Oct 1 '17 at 18:50
  • The Bulk API won't be so useful with Apex due to the asynchronous nature. You might also want to look into the Bulk API 2.0. – Daniel Ballinger Oct 1 '17 at 19:05
  • Just curious - any idea what the update doc means by the line "Records in a single file must be of the same object type." – gorav Oct 1 '17 at 20:01
  • 1
    That is a bit of an odd statement, as you can only do one sObject per normal REST API call. Seems a bit meaningless. I'll try the SalesforceDocs people. – Daniel Ballinger Oct 1 '17 at 21:07
1

As of API v42.2 you can now use the SObject Collections Composite endpoints. There are two major benefits over the existing composite tree & batch endpoints.

1: You can update up to 200 records at a time

2: The updates take place in a single execution context on salesforce. The other composite record create a context PER RECORD, which make it run incredibly slow for big updates.

  • 1
    Thanks I built this out using that approach last month and it's working perfectly. – gorav Mar 31 '18 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.