Merge End Users

Merges the end user specified in the path parameter into the existing end user specified in the request body. Any two end users can be merged with the exception of end users created by sharing agreements. Agents and admins cannot be merged. For more information about how user data is merged, see [Merging a user's duplicate account](https://support.zendesk.com/hc/en-us/articles/4408887695898) in Zendesk help. #### Allowed For * Admins or agents with permission to edit end users

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
 * Merge End Users
8
 * Merges the end user specified in the path parameter into the existing end user specified in the request body.
9

10
Any two end users can be merged with the exception of end users created by sharing agreements.
11

12
Agents and admins cannot be merged.
13

14
For more information about how user data is merged, see [Merging a user's duplicate account](https://support.zendesk.com/hc/en-us/articles/4408887695898) in Zendesk help.
15

16
#### Allowed For
17

18
* Admins or agents with permission to edit end users
19

20
 */
21
export async function main(
22
  auth: Zendesk,
23
  user_id: string,
24
  body: {
25
    user?: {
26
      custom_role_id?: string;
27
      email?: string;
28
      external_id?: string;
29
      identities?: { type?: string; value?: string; [k: string]: unknown }[];
30
      name?: string;
31
      organization?: { name?: string; [k: string]: unknown };
32
      organization_id?: string;
33
      role?: string;
34
      [k: string]: unknown;
35
    };
36
    [k: string]: unknown;
37
  }
38
) {
39
  const url = new URL(
40
    `https://${auth.subdomain}.zendesk.com/api/v2/users/${user_id}/merge`
41
  );
42

43
  const response = await fetch(url, {
44
    method: "PUT",
45
    headers: {
46
      "Content-Type": "application/json",
47
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
48
    },
49
    body: JSON.stringify(body),
50
  });
51
  if (!response.ok) {
52
    const text = await response.text();
53
    throw new Error(`${response.status} ${text}`);
54
  }
55
  return await response.json();
56
}
57