Autocomplete Users

Returns an array of users whose name starts with the value specified in the `name` parameter. It only returns users with no foreign identities. #### 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 Users
8
 * Returns an array of users whose name starts with the value specified in the `name` parameter.
9
It only returns users with no foreign identities.
10

11
#### Allowed For
12

13
* Agents
14

15
 */
16
export async function main(
17
  auth: Zendesk,
18
  name: string | undefined,
19
  field_id: string | undefined,
20
  source: string | undefined
21
) {
22
  const url = new URL(
23
    `https://${auth.subdomain}.zendesk.com/api/v2/users/autocomplete`
24
  );
25
  for (const [k, v] of [
26
    ["name", name],
27
    ["field_id", field_id],
28
    ["source", source],
29
  ]) {
30
    if (v !== undefined && v !== "") {
31
      url.searchParams.append(k, v);
32
    }
33
  }
34
  const response = await fetch(url, {
35
    method: "GET",
36
    headers: {
37
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
38
    },
39
    body: undefined,
40
  });
41
  if (!response.ok) {
42
    const text = await response.text();
43
    throw new Error(`${response.status} ${text}`);
44
  }
45
  return await response.json();
46
}
47