Update the Value of Custom Fields through the Script Console


Bulk update the values of a custom field, saving time manually editing an issue.


I am a project manager, and I have realised that there is a mistake in some of my custom fields. I want to automatically update my Summary custom field value to New Summary. I can use this snippet as part of a larger script to automate this process.

Good to Know

  • Field types include select lists, project pickers, user pickers, checkboxes, radio buttons, etc.


Jira Jira (7.7 - 8.6)

import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.event.type.EventDispatchOption import com.atlassian.jira.issue.fields.CustomField import groovy.transform.Field // the issue key to update @Field final String issueKey = "JRA-1" // the name of a 'single select list' custom field final String selectList = "SelectListA" // the name of a 'multi select list' custom field final String multiSelectList = "MultiSelectA" // name of a 'radio button' custom field final String radioButtonField = "RadioButtons" // name of a 'check box' custom field final String checkboxField = "Checkboxes" // the name of a 'text field' custom field final String textField = "TextFieldA" // the name of a 'user picker' custom field final String userPicker = "UserPicker" // the name of a 'multi user picker' custom field final String multiUserPicker = "MultiUserPickerA" // the name of a 'group picker' custom field final String groupPicker = "GroupPicker" // the name of a 'multi group picker' custom field final String multiGroupPicker = "MultiGroupPicker" // the name of a 'date and time' custom field final String dateTimeField = "First DateTime" // the name of a 'date' custom field final String dateField = "Date" // the name of a 'project picker' custom field final String projectPickerField = "ProjectPicker" // the name of a 'label' picker custom field final String labelField = "LabelField" // name of a 'single version picker' custom field final String versionField = "VersionPicker" // change to 'true' if you want to send an email if the update is successful final boolean sendMail = false def issueService = ComponentAccessor.issueService def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey) assert issue: "Could not find issue with key $issueKey" def issueInputParameters = issueService.newIssueInputParameters().with { // set custom fields with options (select lists, checkboxes, radio buttons) addCustomFieldValue(getSingleCustomFieldByName(selectList).id, *getOptionIdsForFieldByValue(selectList, "BBB")) addCustomFieldValue(getSingleCustomFieldByName(multiSelectList).id, *getOptionIdsForFieldByValue(multiSelectList, "BBB", "CCC")) addCustomFieldValue(getSingleCustomFieldByName(radioButtonField).id, *getOptionIdsForFieldByValue(radioButtonField, "Yes")) addCustomFieldValue(getSingleCustomFieldByName(checkboxField).id, *getOptionIdsForFieldByValue(checkboxField, "Maybe", "Yes")) // set text fields addCustomFieldValue(getSingleCustomFieldByName(textField).id, "New Value") // set user fields addCustomFieldValue(getSingleCustomFieldByName(userPicker).id, "bob") addCustomFieldValue(getSingleCustomFieldByName(multiUserPicker).id, "bob", "alice") // set group fields addCustomFieldValue(getSingleCustomFieldByName(groupPicker).id, "jira-users") addCustomFieldValue(getSingleCustomFieldByName(multiGroupPicker).id, "jira-users", "jira-administrators") // set custom field of type date addCustomFieldValue(getSingleCustomFieldByName(dateTimeField).id, "04/Feb/12 8:47 PM") addCustomFieldValue(getSingleCustomFieldByName(dateField).id, "04/Feb/12") } // set project picker field def project = ComponentAccessor.projectManager.getProjectObjByKey("SSPA") assert project: "Could not find project" issueInputParameters.addCustomFieldValue(getSingleCustomFieldByName(projectPickerField).id, project.id.toString()) // set custom field of type label issueInputParameters.addCustomFieldValue(getSingleCustomFieldByName(labelField).id, "foo", "bar") // set custom field of type version def version = ComponentAccessor.versionManager.getVersions(issue.projectObject).findByName("Version1") assert version: "Could not find version" issueInputParameters.addCustomFieldValue(getSingleCustomFieldByName(versionField).id, version.id.toString()) def updateValidationResult = issueService.validateUpdate(loggedInUser, issue.id, issueInputParameters) assert updateValidationResult.isValid(): updateValidationResult.errorCollection def issueUpdateResult = issueService.update(loggedInUser, updateValidationResult, EventDispatchOption.ISSUE_UPDATED, sendMail) assert issueUpdateResult.isValid(): issueUpdateResult.errorCollection /** * Get a custom field given a custom field name. * If there are than one custom fields with the same name under the same Context then return the first one. * @param fieldName The name of the custom field * @param issue The issue to look for that custom field * @return the custom field, if that exists */ CustomField getSingleCustomFieldByName(String fieldName) { def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey) def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(fieldName) assert customField: "Could not find custom field with name $fieldName" customField } /** * Given a custom field name and option values, retrieve their ids as String * @param customFieldName The name of the custom field * @param values The values in order to get their ids * @return List < String > The ids of the given values */ List getOptionIdsForFieldByValue(String customFieldName, String... values) { def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey) def customField = getSingleCustomFieldByName(customFieldName) ComponentAccessor.optionsManager.getOptions(customField.getRelevantConfig(issue)).findAll { it.value in values.toList() }*.optionId*.toString() }
Discovered an issue? Report it here

Suggested for you

Appears in these Collections