Autocomplete Problems

Returns tickets whose type is "problem" and whose subject contains the string specified in the `text` parameter. You can specify the `text` parameter in the request body rather than the query string. Example: `{"text": "fire"}` #### 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 Problems
8
 * Returns tickets whose type is "problem" and whose subject contains the string specified in the `text` parameter.
9

10
You can specify the `text` parameter in the request body rather than the query string. Example:
11

12
`{"text": "fire"}`
13

14
#### Allowed For
15

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