Get sources by target

Returns a list of source objects whose values are populated with the id of a related target object. For example, if you have a lookup field called "Success Manager" on a ticket, this endpoint can answer the question, "What tickets (sources) is this user (found by `target_type` and `target_id`) assigned as the 'Success Manager' (field referenced by `field_id`)?" #### 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
 * Get sources by target
8
 * Returns a list of source objects whose values are populated with the id of a related target object.  For example,
9
if you have a lookup field called "Success Manager" on a ticket, this endpoint can answer the question,
10
"What tickets (sources) is this user (found by `target_type` and `target_id`)
11
assigned as the 'Success Manager' (field referenced by `field_id`)?"
12

13
#### Allowed For
14

15
* Agents
16

17
 */
18
export async function main(
19
  auth: Zendesk,
20
  target_type: string,
21
  target_id: string,
22
  field_id: string,
23
  source_type: string
24
) {
25
  const url = new URL(
26
    `https://${auth.subdomain}.zendesk.com/api/v2/${target_type}/${target_id}/relationship_fields/${field_id}/${source_type}`
27
  );
28

29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
33
    },
34
    body: undefined,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42