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

YouTrack自定义工作流中无法设置自定义字段值问题求助

YouTrack脚本更新Estimation字段报错的解决办法

报错原因

出现「Cannot set value to custom field Estimation」错误,主要是三个原因:

  • 脚本未在requirements中声明Estimation字段,YouTrack默认限制对未声明字段的写入操作
  • Estimation字段类型不匹配:YouTrack默认的预估时长字段是工作项类型,而非普通数字字段,直接赋值数字会触发类型兼容错误
  • 计算出的天数差可能为浮点数,不符合Estimation字段的数值格式要求

修正后的完整代码

/**
 * 当截止日期变更时,若存在开始日期,自动计算天数差并填充预估时长字段
 */

const entities = require('@jetbrains/youtrack-scripting-api/entities');

exports.rule = entities.Issue.onChange({
  title: 'Due-date-set',
  guard: (ctx) => {
    const issue = ctx.issue;
    // 增加判断:确保开始日期存在,避免计算出NaN
    return issue.isChanged('Due Date') && issue.fields.StartDate !== null;
  },
  action: (ctx) => {
    const issue = ctx.issue;
    // 计算天数差并转换为整数(四舍五入,也可使用Math.floor取整)
    const diffMs = issue.fields.DueDate - issue.fields.StartDate;
    const diffDays = Math.round(diffMs / (1000 * 60 * 60 * 24));
    
    // 根据实际字段类型二选一处理
    if (diffDays > 0) {
      // 情况1:Estimation是工作项类型(YouTrack默认预估字段)
      const estimationItem = ctx.Estimation.create(diffDays, ctx.Hours);
      issue.fields.Estimation = estimationItem;
      
      // 情况2:Estimation是自定义普通数字字段(整数/浮点数)
      // issue.fields.Estimation = diffDays;
    }
  },
  requirements: {
    DueDate: {
      type: entities.Field.dateType,
      name: 'Due Date'
    },
    StartDate: {
      type: entities.Field.dateType,
      name: 'Start Date'
    },
    // 必须声明Estimation字段,类型与系统实际字段一致
    Estimation: {
      // 工作项类型用此配置
      type: entities.Field.workItemType,
      // 普通数字字段用下面的二选一配置
      // type: entities.Field.integerType,
      name: 'Estimation'
    },
    // 工作项类型需指定时长单位,名称与系统内一致
    Hours: {
      type: entities.Iteration,
      name: 'Hours'
    }
  }
});

关键修改点说明

  1. 补全字段声明:在requirements中添加Estimation字段定义,类型必须与YouTrack系统内的实际字段类型匹配——默认预估字段用workItemType,自定义数字字段用integerType或floatType。
  2. 数值格式处理:将毫秒差转换为天数后转成整数,避免浮点数引发的类型错误。
  3. 工作项字段赋值逻辑:若为工作项类型的Estimation,不能直接赋值数字,需通过ctx.Estimation.create(数值, 单位)创建对应工作项对象后再赋值。
  4. 空值校验:在guard中增加开始日期非空判断,防止空值计算导致脚本崩溃。

额外检查项

  • 确认字段类型:进入项目设置→自定义字段,查看Estimation的实际类型,确保脚本配置的type与之匹配。
  • 权限验证:确保脚本规则拥有修改Estimation字段的权限。
  • 工作日计算:若需排除节假日/非工作日,可使用YouTrack内置的ctx.calendar.daysBetween(startDate, dueDate)方法计算实际工作日数,比自然日计算更精准。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 06:33:13