Snippets

Adaptavist Jira Cloud - Script Console - Bulk Import Custom Field Values

Created by Kristian Walker last modified
/*
* This example script console script shows how to bulk import custom field values . 
* "All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner
* license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed or
* amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
*/

// Specify the name of the custom field below
def customtFieldName = ''

// Get the Custom field to get the option value from
def customField = get("/rest/api/2/field")
        .asObject(List)
        .body
        .find {
    (it as Map).name == customtFieldName
} as Map

// Check if the custom field returns a valid field and is not null
assert customField != null

// Get all contexts for the custom field
def getContexts = get("/rest/api/3/field/${customField.id}/context")
        .asObject(Map)

// Check if we fetched the custom field contexts correctly
assert getContexts.status >= 200 && getContexts.status < 300

// Get the Id for the default context
def defaultContextId = getContexts.body.values[0].id;


// Provide an array for each field option which has a key value pair of "value":"OptionValueHere"
def optionsArray = [
    // Replace with real option values below
    ["value": "Option 1"], 
    ["value": "Option 2"],
    ["value": "Option 3"]
]

// Set the new custom field options
def addCustomFieldOptions = post("/rest/api/3/field/${customField.id}/context/${defaultContextId}/option")
        .header('Content-Type', 'application/json')
        .body(options:optionsArray)
        .asJson()

// Validate that the custom field options were added correclty
assert addCustomFieldOptions.status >= 200 && addCustomFieldOptions.status < 300

// Log out what field was updated
logger.info("The custom field named ${customtFieldName} has been updated with the new values provided")

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.