Search Custom Object Records

Returns an array of custom object records that meet the search criteria #### Pagination * Cursor pagination only. * Returns the records sorted by relevancy with page limits. Without a `sort` parameter, only the first 10,000 records are returned. With a `sort` parameter, all records are returned. #### Allowed For * Agents

Script zendesk Verified

by hugo697 ยท 11/7/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 377 days ago
1
type Zendesk = {
2
  username: string;
3
  password: string;
4
  subdomain: string;
5
};
6
/**
7
 * Search Custom Object Records
8
 * Returns an array of custom object records that meet the search criteria
9

10
#### Pagination
11

12
* Cursor pagination only.
13
* Returns the records sorted by relevancy with page limits. Without a `sort` parameter, only the first 10,000 records are returned. With a `sort` parameter, all records are returned.
14
#### Allowed For
15
* Agents
16
 */
17
export async function main(
18
  auth: Zendesk,
19
  custom_object_key: string,
20
  query: string | undefined,
21
  sort: string | undefined,
22
  page_before_: string | undefined,
23
  page_after_: string | undefined,
24
  page_size_: string | undefined
25
) {
26
  const url = new URL(
27
    `https://${auth.subdomain}.zendesk.com/api/v2/custom_objects/${custom_object_key}/records/search`
28
  );
29
  for (const [k, v] of [
30
    ["query", query],
31
    ["sort", sort],
32
    ["page[before]", page_before_],
33
    ["page[after]", page_after_],
34
    ["page[size]", page_size_],
35
  ]) {
36
    if (v !== undefined && v !== "") {
37
      url.searchParams.append(k, v);
38
    }
39
  }
40
  const response = await fetch(url, {
41
    method: "GET",
42
    headers: {
43
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
44
    },
45
    body: undefined,
46
  });
47
  if (!response.ok) {
48
    const text = await response.text();
49
    throw new Error(`${response.status} ${text}`);
50
  }
51
  return await response.json();
52
}
53