0

SOSL Search

by
Published 8 days ago

Run a SOSL search across one or more objects, e.g. FIND {Acme} IN Name Fields RETURNING Account(Id, Name), Contact(Id, Name).

Script salesforce Verified

The script

Submitted by hugo989 Bun
Verified 9 days ago
1
//native
2

3
/**
4
 * SOSL Search
5
 * Run a SOSL search across one or more objects, e.g. FIND {Acme} IN Name Fields RETURNING Account(Id, Name), Contact(Id, Name).
6
 */
7
export async function main(auth: RT.Salesforce, search: string) {
8
  const apiVersion = auth.api_version || "v60.0"
9
  const url = new URL(`${auth.instance_url}/services/data/${apiVersion}/search`)
10
  url.searchParams.append("q", search)
11

12
  const response = await fetch(url, {
13
    method: "GET",
14
    headers: {
15
      Authorization: `Bearer ${auth.token}`,
16
      Accept: "application/json",
17
    },
18
  })
19

20
  if (!response.ok) {
21
    throw new Error(`${response.status} ${await response.text()}`)
22
  }
23

24
  return await response.json()
25
}
26