Search Organizations by External ID

If you set the `external_id` value of an organization to associate it to an external record, you can use the external id to search for the organization. #### Allowed For: * Admins * Agents assigned to a custom role with permissions to add or modify organizations (Enterprise only) See [Creating custom agent roles](https://support.zendesk.com/hc/en-us/articles/203662026#topic_cxn_hig_bd) in the Support Help Center.

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
 * Search Organizations by External ID
8
 * If you set the `external_id` value of an organization to associate it to an external record, you can use the external id to search for the organization.
9

10
#### Allowed For:
11

12
* Admins
13
* Agents assigned to a custom role with permissions to add or modify organizations (Enterprise only)
14

15
See [Creating custom agent roles](https://support.zendesk.com/hc/en-us/articles/203662026#topic_cxn_hig_bd) in the Support Help Center.
16

17
 */
18
export async function main(auth: Zendesk, external_id: string | undefined) {
19
  const url = new URL(
20
    `https://${auth.subdomain}.zendesk.com/api/v2/organizations/search`
21
  );
22
  for (const [k, v] of [["external_id", external_id]]) {
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