You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jira 8.20.6自定义脚本后置函数报错:无法解析ComponentManager类

问题解决:Jira工作流脚本后置函数字段自增报错处理

报错翻译

无法解析ComponentManager类

问题原因

ComponentManager在Jira 7及以上版本已被废弃,Jira 8.20.6不再支持该类的直接调用,而你已经导入了替代它的ComponentAccessor却未使用。此外原脚本仅处理单个字段,未覆盖你需要的Program、Label、External三个文本类型字段,且文本转数值的逻辑存在潜在异常风险。

修正后的Groovy脚本

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

// 定义需要自增的三个自定义字段ID(替换为你实际的字段ID)
def customFieldIds = [
    "customfield_xxxx", // Program字段ID
    "customfield_yyyy", // Label字段ID
    "customfield_zzzz"  // External字段ID
]

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def changeHolder = new DefaultIssueChangeHolder()

customFieldIds.each { fieldId ->
    CustomField cf = customFieldManager.getCustomFieldObject(fieldId)
    if (!cf) {
        log.warn("未找到ID为${fieldId}的自定义字段")
        return
    }

    // 获取当前字段值,处理文本类型转换
    def currentValue = issue.getCustomFieldValue(cf)?.toString()?.trim()
    int val = 1
    if (currentValue) {
        try {
            val = Integer.parseInt(currentValue) + 1
        } catch (NumberFormatException e) {
            log.error("字段${cf.name}的值${currentValue}不是有效数字,重置为1")
        }
    }

    // 更新字段值(文本类型需转为字符串存储)
    cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), val.toString()), changeHolder)
}

关键修改点

  • 用ComponentAccessor.getCustomFieldManager()替代废弃的ComponentManager.getInstance()获取自定义字段管理器
  • 批量处理三个目标字段,通过循环简化代码
  • 增加文本转数值的异常处理,避免字段值非数字时脚本崩溃
  • 适配文本类型字段,将自增后的数值转为字符串再更新

内容的提问来源于stack exchange,提问作者Manu Mishra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 00:01:06