Update custom field

Updates a custom field. **[Permissions](#permissions) required:** *Administer Jira* [global permission](https://confluence.atlassian.com/x/x4dKLg).

Script jira Verified

by hugo697 ยท 11/2/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Update custom field
8
 * Updates a custom field.
9

10
**[Permissions](#permissions) required:** *Administer Jira* [global permission](https://confluence.atlassian.com/x/x4dKLg).
11
 */
12
export async function main(
13
  auth: Jira,
14
  fieldId: string,
15
  body: {
16
    description?: string;
17
    name?: string;
18
    searcherKey?:
19
      | "com.atlassian.jira.plugin.system.customfieldtypes:cascadingselectsearcher"
20
      | "com.atlassian.jira.plugin.system.customfieldtypes:daterange"
21
      | "com.atlassian.jira.plugin.system.customfieldtypes:datetimerange"
22
      | "com.atlassian.jira.plugin.system.customfieldtypes:exactnumber"
23
      | "com.atlassian.jira.plugin.system.customfieldtypes:exacttextsearcher"
24
      | "com.atlassian.jira.plugin.system.customfieldtypes:grouppickersearcher"
25
      | "com.atlassian.jira.plugin.system.customfieldtypes:labelsearcher"
26
      | "com.atlassian.jira.plugin.system.customfieldtypes:multiselectsearcher"
27
      | "com.atlassian.jira.plugin.system.customfieldtypes:numberrange"
28
      | "com.atlassian.jira.plugin.system.customfieldtypes:projectsearcher"
29
      | "com.atlassian.jira.plugin.system.customfieldtypes:textsearcher"
30
      | "com.atlassian.jira.plugin.system.customfieldtypes:userpickergroupsearcher"
31
      | "com.atlassian.jira.plugin.system.customfieldtypes:versionsearcher";
32
  }
33
) {
34
  const url = new URL(
35
    `https://${auth.domain}.atlassian.net/rest/api/2/field/${fieldId}`
36
  );
37

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