Autocomplete Organizations

Returns an array of organizations whose name starts with the value specified in the `name` parameter. #### Pagination * Offset pagination only See Using Offset Pagination. #### 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
 * Autocomplete Organizations
8
 * Returns an array of organizations whose name starts with the
9
value specified in the `name` parameter.
10

11
#### Pagination
12

13
* Offset pagination only
14

15
See Using Offset Pagination.
16

17
#### Allowed For
18

19
* Agents
20

21
 */
22
export async function main(
23
  auth: Zendesk,
24
  name: string | undefined,
25
  field_id: string | undefined,
26
  source: string | undefined
27
) {
28
  const url = new URL(
29
    `https://${auth.subdomain}.zendesk.com/api/v2/organizations/autocomplete`
30
  );
31
  for (const [k, v] of [
32
    ["name", name],
33
    ["field_id", field_id],
34
    ["source", source],
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