0

Parameterized Search

by
Published 8 days ago

Search for a term without writing SOSL. Optionally restrict to one object and a chosen set of fields to return (multi-select).

Script salesforce Verified

The script

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

3
export type DynSelect_sobject = string
4
export type DynMultiselect_fields = string[]
5

6
// Dropdown of the org's objects (global describe).
7
export async function sobject(auth: RT.Salesforce) {
8
  const apiVersion = auth.api_version || "v60.0"
9
  const response = await fetch(
10
    `${auth.instance_url}/services/data/${apiVersion}/sobjects/`,
11
    {
12
      headers: {
13
        Authorization: `Bearer ${auth.token}`,
14
        Accept: "application/json",
15
      },
16
    }
17
  )
18
  if (!response.ok) {
19
    throw new Error(`${response.status} ${await response.text()}`)
20
  }
21
  const { sobjects } = (await response.json()) as {
22
    sobjects: { name: string; label: string }[]
23
  }
24
  return sobjects
25
    .map((o) => ({ value: o.name, label: `${o.label} (${o.name})` }))
26
    .sort((a, b) => a.label.localeCompare(b.label))
27
}
28

29
// Dependent multi-select: fields of the chosen object (only relevant when sobject is set).
30
export async function fields(auth: RT.Salesforce, sobject: DynSelect_sobject) {
31
  if (!sobject) return []
32
  const apiVersion = auth.api_version || "v60.0"
33
  const response = await fetch(
34
    `${auth.instance_url}/services/data/${apiVersion}/sobjects/${sobject}/describe`,
35
    {
36
      headers: {
37
        Authorization: `Bearer ${auth.token}`,
38
        Accept: "application/json",
39
      },
40
    }
41
  )
42
  if (!response.ok) {
43
    throw new Error(`${response.status} ${await response.text()}`)
44
  }
45
  const { fields: fieldList } = (await response.json()) as {
46
    fields: { name: string; label: string }[]
47
  }
48
  return fieldList
49
    .map((f) => ({ value: f.name, label: `${f.label} (${f.name})` }))
50
    .sort((a, b) => a.label.localeCompare(b.label))
51
}
52

53
/**
54
 * Parameterized Search
55
 * Search for a term without writing SOSL. Optionally restrict to one object and a chosen set of fields to return.
56
 */
57
export async function main(
58
  auth: RT.Salesforce,
59
  q: string,
60
  sobject: DynSelect_sobject | undefined,
61
  fields: DynMultiselect_fields | undefined
62
) {
63
  const apiVersion = auth.api_version || "v60.0"
64
  const url = new URL(
65
    `${auth.instance_url}/services/data/${apiVersion}/parameterizedSearch`
66
  )
67
  url.searchParams.append("q", q)
68
  if (sobject !== undefined && sobject !== "") {
69
    url.searchParams.append("sobject", sobject)
70
    if (fields && fields.length > 0) {
71
      url.searchParams.append(`${sobject}.fields`, fields.join(","))
72
    }
73
  }
74

75
  const response = await fetch(url, {
76
    method: "GET",
77
    headers: {
78
      Authorization: `Bearer ${auth.token}`,
79
      Accept: "application/json",
80
    },
81
  })
82

83
  if (!response.ok) {
84
    throw new Error(`${response.status} ${await response.text()}`)
85
  }
86

87
  return await response.json()
88
}
89