SOQL: single pages (soql, query-all, next-page), an auto-paging helper (soql-records), and literal escaping for building queries from untrusted values (escape, escape-like, literal).

Functions

escape

fn (value: Str): Str

Escape a string for use inside a quoted SOQL literal: backslash, single and double quotes, and the control characters \n \r \t \b \f, per the SOQL escape-sequence rules. Wrap the result in single quotes yourself, or use literal.

Example

name "O'Brien"
::salesforce::query/soql(`SELECT Id FROM Contact WHERE LastName = '${::salesforce::query/escape(name)}'`)

escape-like

fn (value: Str): Str

Like escape, and also escapes the LIKE wildcards % and _ so the value matches literally. Add your own % around it: `Name LIKE '%${escape-like(term)}%'`.

Example

::salesforce::query/escape-like("50%_off")  // "50\\%\\_off"

literal

fn (value: Any): Str

Render a value as a SOQL literal: Str → quoted and escaped, Int/Dec → as is, Bool → true/false, null → null, Vec → ('a', 'b') for IN. Date and datetime literals are unquoted in SOQL: interpolate those directly.

Example

ids ["001A", "001B"]
`SELECT Id, Name FROM Account WHERE Id IN ${::salesforce::query/literal(ids)}`

next-page

fn (next-records-url: Str): Map
fn (next-records-url: Str, opts: Map): Map

Fetch the next page of a SOQL result from its nextRecordsUrl (a full /services/data/vXX.X/query/... path, used as is).

Example

page ::salesforce::query/soql("SELECT Id FROM Contact")
more if(page.done, null, ::salesforce::query/next-page(page.nextRecordsUrl))

query-all

fn (query: Str): Map
fn (query: Str, opts: Map): Map

GET /queryAll?q= - Like soql, but includes deleted (recycle bin) and archived records (IsDeleted = true rows, archived Tasks and Events). One page; see soql-records with include-deleted for every page.

Example

out ::salesforce::query/query-all("SELECT Id FROM Lead WHERE IsDeleted = true")
out.records

query-headers

fn (options: Map): Map

Request options for a query: batch-size (200 to 2000) becomes Sforce-Query-Options: batchSize=N. Exposed for testing.

Example

::salesforce::query/query-headers({batch-size: 500})  // {headers: {Sforce-Query-Options: "batchSize=500"}}
::salesforce::query/query-headers({})  // {}

query-path

fn (query: Str, include-deleted: Bool): Str

The /query (or /queryAll, with include-deleted) path for a SOQL string. Exposed for testing.

Example

::salesforce::query/query-path("SELECT Id FROM Lead", false)  // "/query?q=SELECT+Id+FROM+Lead"
::salesforce::query/query-path("SELECT Id FROM Lead", true)  // "/queryAll?q=SELECT+Id+FROM+Lead"

soql

fn (query: Str): Map
fn (query: Str, options: Map): Map
fn (query: Str, options: Map, opts: Map): Map

GET /query?q= - Run a SOQL query and return one page: {totalSize, done, records, nextRecordsUrl?}. Pass nextRecordsUrl to next-page while done is false, or use soql-records to collect every page. options: include-deleted (use /queryAll, which also returns deleted and archived rows), batch-size (200 to 2000 records per page; a hint Salesforce may lower).

Example

open ::salesforce::query/soql("SELECT Id, Name, StageName FROM Opportunity WHERE IsClosed = false")
open.totalSize

soql-records

fn (query: Str): Vec
fn (query: Str, options: Map): Vec
fn (query: Str, options: Map, opts: Map): Vec

Run a SOQL query and follow nextRecordsUrl until done, returning the records as one Vec. options: max-records (stop once this many are collected; strongly advised for large objects), include-deleted, batch-size. Each page is one API call.

Example

contacts ::salesforce::query/soql-records("SELECT Id, Email FROM Contact WHERE Email != null", {max-records: 5000})
length(contacts)