0

Create Custom Field Options

by
Published Nov 2, 2023

Add options to a select-type custom field in one of its contexts. Requires the Administer Jira permission.

Script jira Verified

The script

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

3
async function get(auth: RT.Jira, path: string) {
4
  const response = await fetch(`https://${auth.domain}.atlassian.net${path}`, {
5
    headers: {
6
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
7
      Accept: "application/json",
8
    },
9
  })
10
  if (!response.ok) {
11
    throw new Error(`${response.status} ${await response.text()}`)
12
  }
13
  return await response.json()
14
}
15

16
export type DynSelect_field_id = string
17

18
// Resolver: the site's custom fields (first 100).
19
export async function field_id(auth: RT.Jira) {
20
  const { values } = await get(
21
    auth,
22
    "/rest/api/3/field/search?type=custom&maxResults=100"
23
  )
24
  return values.map((f: { id: string; name: string }) => ({
25
    value: f.id,
26
    label: `${f.name} (${f.id})`,
27
  }))
28
}
29

30
export type DynSelect_context_id = string
31

32
// Resolver: the contexts of the chosen custom field.
33
export async function context_id(auth: RT.Jira, field_id: string) {
34
  if (!field_id) return []
35
  const { values } = await get(auth, `/rest/api/3/field/${field_id}/context`)
36
  return values.map((c: { id: string; name: string }) => ({
37
    value: c.id,
38
    label: c.name,
39
  }))
40
}
41

42
/**
43
 * Create Custom Field Options
44
 * Add options to a select-type custom field in one of its contexts. Requires the Administer Jira permission.
45
 */
46
export async function main(
47
  auth: RT.Jira,
48
  field_id: DynSelect_field_id,
49
  context_id: DynSelect_context_id,
50
  options: { value: string; disabled?: boolean; optionId?: string }[]
51
) {
52
  const url = new URL(
53
    `https://${auth.domain}.atlassian.net/rest/api/3/field/${field_id}/context/${context_id}/option`
54
  )
55

56
  const response = await fetch(url, {
57
    method: "POST",
58
    headers: {
59
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
60
      "Content-Type": "application/json",
61
      Accept: "application/json",
62
    },
63
    body: JSON.stringify({ options }),
64
  })
65

66
  if (!response.ok) {
67
    throw new Error(`${response.status} ${await response.text()}`)
68
  }
69

70
  return await response.json()
71
}
72

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 525 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Create custom field options (context)
    8
     * Creates options and, where the custom select field is of the type Select List (cascading), cascading options for a custom select field.
    9
     */
    10
    export async function main(
    11
      auth: Jira,
    12
      fieldId: string,
    13
      contextId: string,
    14
      body: { options?: { disabled?: boolean; optionId?: string; value: string }[] }
    15
    ) {
    16
      const url = new URL(
    17
        `https://${auth.domain}.atlassian.net/rest/api/2/field/${fieldId}/context/${contextId}/option`
    18
      );
    19
    
    
    20
      const response = await fetch(url, {
    21
        method: "POST",
    22
        headers: {
    23
          "Content-Type": "application/json",
    24
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    25
        },
    26
        body: JSON.stringify(body),
    27
      });
    28
      if (!response.ok) {
    29
        const text = await response.text();
    30
        throw new Error(`${response.status} ${text}`);
    31
      }
    32
      return await response.json();
    33
    }
    34