如何基于Jira的'Bulk Fix Resolutions'脚本批量更新自定义字段值?
Great question! Adapting that script to target a custom field instead of the resolution status is totally doable—let’s walk through the key adjustments you’ll need to make:
1. Grab Your Custom Field’s Unique Identifier
First up, you need the ID or key of your target custom field. Here’s how to get it:
- Head to Jira Settings → Issues → Custom Fields
- Find your field, click the three-dot menu → View Details
- Copy the
Custom Field ID(it’ll look likecustomfield_12345) - Alternatively, use Jira’s REST API to list all fields:
Search the response for your field’s name to grab itsGET /rest/api/3/fieldidorkey.
2. Swap Resolution Field Logic for Custom Field Logic
The original script centers on Jira’s built-in resolution field—you’ll need to replace that with code targeting your custom field. Here’s a typical before/after example:
Original Resolution-focused snippet:
def resolution = ComponentAccessor.resolutionManager.getResolutionById(resolutionId) issue.setResolution(resolution)
Updated Custom Field snippet:
// Replace with your actual custom field ID def customFieldId = "customfield_12345" def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(customFieldId) // Adjust based on your custom field's type if (customField.type.name == "Select") { // For single-select fields: fetch the target option object def targetOption = ComponentAccessor.optionsManager.getOptions(customField.getRelevantConfig(issue)) .find { it.value == "Your Desired Value" } issue.setCustomFieldValue(customField, targetOption) } else if (customField.type.name == "Text Field") { // For text fields: directly set the string value issue.setCustomFieldValue(customField, "Your Custom Text") }
3. Update the Script’s Input/UI (If Using ScriptRunner)
If this is a ScriptRunner bulk script, the original likely has a UI dropdown for selecting a resolution. You’ll need to tweak this to let users input or select the custom field value:
- Replace the resolution dropdown with one populated by your custom field’s options (for select-type fields)
- Add a text input box if your custom field accepts free-form text
- Update the script’s parameter definitions to map to the new UI elements
4. Handle Field Type Specifics
Different custom field types need different handling—don’t skip this:
- Single/Multi-select: You must pass an
Optionobject (not just a string) - Text/Number/Date: You can pass the raw value (string, integer, Date object)
- User Picker: Pass a
ApplicationUserobject (retrieve viaComponentAccessor.userManager.getUserByName("username"))
5. Test Before Going Bulk
Never run a modified script on a large batch of issues first:
- Test with 1-2 test issues to confirm the custom field updates correctly
- Double-check permissions (ensure the script has edit access to both the field and the target issues)
- Verify the field is available in the context of all issues you’re targeting
内容的提问来源于stack exchange,提问作者user2055729

