Multi-record calls. sObject collections (create-records, update-records, upsert-records, delete-records, get-records) write up to 200 records per API call; larger inputs are split into 200-record chunks unless all-or-none is set, which needs every record in one call. composite runs up to 25 dependent subrequests in one round trip, with @{referenceId.field} references between them.

Collection calls return one result per record, in order: {id, success, errors: [{statusCode, message, fields}]} (plus created for upserts). With all-or-none false (the default) a failing record does not roll back the others, so check success.

Values

MAX_COLLECTION

MAX_COLLECTION: Int 200

MAX_RETRIEVE

MAX_RETRIEVE: Int 2000

MAX_SUBREQUESTS

MAX_SUBREQUESTS: Int 25

Functions

build-collection-body

fn (sobject-type: Str, records: Vec, all-or-none: Bool): Map

The {allOrNone, records} body for collection create/update/upsert. Exposed for testing.

Example

::salesforce::composite/build-collection-body("Contact", [{LastName: "Lovelace"}], true)
// {allOrNone: true, records: [{LastName: "Lovelace", attributes: {type: "Contact"}}]}

build-composite-body

fn (subrequests: Vec, options: Map): Map

The /composite body: {allOrNone, collateSubrequests?, compositeRequest}; an Err over 25 subrequests. Exposed for testing.

Example

::salesforce::composite/build-composite-body([], {all-or-none: true})  // {allOrNone: true, compositeRequest: []}

chunks

fn (items: Vec, size: Int, all-or-none: Bool): Vec

Split items into groups of at most size; an Err when all-or-none is set and they do not fit in one group (an all-or-none call cannot span requests). Exposed for testing.

Example

::salesforce::composite/chunks([1, 2, 3], 2, false)  // [[1, 2], [3]]
is-err(::salesforce::composite/chunks([1, 2, 3], 2, true))  // true

composite

fn (subrequests: Vec): Map
fn (subrequests: Vec, options: Map): Map
fn (subrequests: Vec, options: Map, opts: Map): Map

POST /composite - Run up to 25 subrequests in order in one call. Returns {compositeResponse: [{body, httpStatusCode, referenceId}]}. options: all-or-none (roll back everything if any subrequest fails), collate-subrequests.

Example

::c ::salesforce::composite

out ::c/composite([
  ::c/subrequest("POST", "/sobjects/Account", "acct", {Name: "Acme"}),
  ::c/subrequest("POST", "/sobjects/Contact", "ada", {LastName: "Lovelace", AccountId: "@{acct.id}"})
], {all-or-none: true})
map(out.compositeResponse, (r) { r.httpStatusCode })

create-records

fn (sobject-type: Str, records: Vec): Vec
fn (sobject-type: Str, records: Vec, options: Map): Vec
fn (sobject-type: Str, records: Vec, options: Map, opts: Map): Vec

POST /composite/sobjects - Create records of one type, 200 per call. options: all-or-none (roll back the whole call on any failure).

Example

results ::salesforce::composite/create-records("Contact", [
  {LastName: "Lovelace", Email: "ada@example.com"},
  {LastName: "Hopper", Email: "grace@example.com"}
])
map(results, (r) { r.id })

delete-path

fn (ids: Vec, all-or-none: Bool): Str

The /composite/sobjects?ids=...&allOrNone=... path for a delete. Exposed for testing.

Example

::salesforce::composite/delete-path(["001A", "001B"], false)
// "/composite/sobjects?ids=001A%2C001B&allOrNone=false"

delete-records

fn (ids: Vec): Vec
fn (ids: Vec, options: Map): Vec
fn (ids: Vec, options: Map, opts: Map): Vec

DELETE /composite/sobjects?ids= - Delete records (of any types) by id, 200 per call; they go to the recycle bin. options: all-or-none.

Example

results ::salesforce::composite/delete-records(["003A", "003B"])
filter(results, (r) { not(r.success) })

get-records

fn (sobject-type: Str, ids: Vec, fields: Vec): Vec
fn (sobject-type: Str, ids: Vec, fields: Vec, opts: Map): Vec

POST /composite/sobjects/{type} - Retrieve up to 2000 records of one type by id with the given fields, in id order (null for ids that do not exist). Larger id lists are split.

Example

rows ::salesforce::composite/get-records("Contact", ["003A", "003B"], ["Id", "Email"])
map(filter(rows, is-some), (r) { r.Email })

subrequest

fn (method: Str, path: Str, reference-id: Str): Map
fn (method: Str, path: Str, reference-id: Str, body: Any): Map
fn (method: Str, path: Str, reference-id: Str, body: Any, opts: Map): Map

One composite subrequest: method, path (relative to the data API root, like ::salesforce::api/json; it is expanded to the full /services/data/vXX.X/... URL), a reference-id later subrequests can cite as @{reference-id.id}, and an optional body. The 5-arity form builds the URL with the api-version in opts; pass the same opts to composite.

Example

::salesforce::composite/subrequest("POST", "/sobjects/Account", "acct", {Name: "Acme"}, {api-version: "v66.0"})
// {method: "POST", url: "/services/data/v66.0/sobjects/Account", referenceId: "acct", body: {Name: "Acme"}}

update-records

fn (sobject-type: Str, records: Vec): Vec
fn (sobject-type: Str, records: Vec, options: Map): Vec
fn (sobject-type: Str, records: Vec, options: Map, opts: Map): Vec

PATCH /composite/sobjects - Update records of one type by Id (each record must carry Id), 200 per call. options: all-or-none.

Example

results ::salesforce::composite/update-records("Contact", [
  {Id: "003A", Title: "CTO"},
  {Id: "003B", Title: "VP Eng"}
], {all-or-none: true})
map(results, (r) { r.success })

upsert-records

fn (sobject-type: Str, external-id-field: Str, records: Vec): Vec
fn (sobject-type: Str, external-id-field: Str, records: Vec, options: Map): Vec
fn (sobject-type: Str, external-id-field: Str, records: Vec, options: Map, opts: Map): Vec

PATCH /composite/sobjects/{type}/{externalIdField} - Create or update records by an external id field (each record must carry that field), 200 per call. Results include created. options: all-or-none.

Example

::salesforce::composite/upsert-records("Account", "External_Id__c", [
  {External_Id__c: "acme", Name: "Acme"},
  {External_Id__c: "globex", Name: "Globex"}
])

with-type

fn (sobject-type: Str, records: Vec): Vec

Add attributes: {type} to each record (collections need it) unless the record already carries one. Exposed for testing.

Example

::salesforce::composite/with-type("Contact", [{LastName: "Lovelace"}])
// [{LastName: "Lovelace", attributes: {type: "Contact"}}]