0

List View Tickets

by
Published Nov 7, 2023

List the tickets in a view, in the view's own order. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.

Script zendesk Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
export type DynSelect_view_id = string
4

5
// Resolver: the agent's active views (first 100).
6
export async function view_id(auth: RT.Zendesk) {
7
  const response = await fetch(
8
    `https://${auth.subdomain}.zendesk.com/api/v2/views?active=true&page[size]=100`,
9
    {
10
      headers: {
11
        Authorization: auth.token
12
          ? `Bearer ${auth.token}`
13
          : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
14
        Accept: "application/json",
15
      },
16
    }
17
  )
18
  if (!response.ok) {
19
    throw new Error(`${response.status} ${await response.text()}`)
20
  }
21
  const { views } = await response.json()
22
  return views.map((v: { id: number; title: string }) => ({
23
    value: String(v.id),
24
    label: v.title,
25
  }))
26
}
27

28
/**
29
 * List View Tickets
30
 * List the tickets in a view, in the view's own order. Cursor-paged: pass `meta.after_cursor` as `cursor` while `meta.has_more` is true.
31
 */
32
export async function main(
33
  auth: RT.Zendesk,
34
  view_id: DynSelect_view_id,
35
  page_size: number | undefined,
36
  cursor: string | undefined
37
) {
38
  const url = new URL(
39
    `https://${auth.subdomain}.zendesk.com/api/v2/views/${view_id}/tickets`
40
  )
41
  url.searchParams.append("page[size]", String(page_size || 100))
42
  if (cursor) {
43
    url.searchParams.append("page[after]", cursor)
44
  }
45

46
  const response = await fetch(url, {
47
    headers: {
48
      Authorization: auth.token
49
        ? `Bearer ${auth.token}`
50
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
51
      Accept: "application/json",
52
    },
53
  })
54

55
  if (!response.ok) {
56
    throw new Error(`${response.status} ${await response.text()}`)
57
  }
58

59
  return await response.json()
60
}
61

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 528 days ago
    1
    type Zendesk = {
    2
      username: string;
    3
      password: string;
    4
      subdomain: string;
    5
    };
    6
    /**
    7
     * List Tickets From a View
    8
     * #### Allowed For
    9
    
    
    10
    * Agents
    11
    
    
    12
     */
    13
    export async function main(
    14
      auth: Zendesk,
    15
      view_id: string,
    16
      sort_by: string | undefined,
    17
      sort_order: string | undefined
    18
    ) {
    19
      const url = new URL(
    20
        `https://${auth.subdomain}.zendesk.com/api/v2/views/${view_id}/tickets`
    21
      );
    22
      for (const [k, v] of [
    23
        ["sort_by", sort_by],
    24
        ["sort_order", sort_order],
    25
      ]) {
    26
        if (v !== undefined && v !== "") {
    27
          url.searchParams.append(k, v);
    28
        }
    29
      }
    30
      const response = await fetch(url, {
    31
        method: "GET",
    32
        headers: {
    33
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    34
        },
    35
        body: undefined,
    36
      });
    37
      if (!response.ok) {
    38
        const text = await response.text();
    39
        throw new Error(`${response.status} ${text}`);
    40
      }
    41
      return await response.json();
    42
    }
    43