Execute View

Returns the column titles and the rows of the specified view.

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
 * Execute View
8
 * Returns the column titles and the rows of the specified view.
9
 */
10
export async function main(
11
  auth: Zendesk,
12
  view_id: string,
13
  sort_by: string | undefined,
14
  sort_order: string | undefined
15
) {
16
  const url = new URL(
17
    `https://${auth.subdomain}.zendesk.com/api/v2/views/${view_id}/execute`
18
  );
19
  for (const [k, v] of [
20
    ["sort_by", sort_by],
21
    ["sort_order", sort_order],
22
  ]) {
23
    if (v !== undefined && v !== "") {
24
      url.searchParams.append(k, v);
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "GET",
29
    headers: {
30
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
31
    },
32
    body: undefined,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.json();
39
}
40