[Solved]Trying to set subtasks as canceled when their parent is set to canceled

Hello, I've been trying to create a workflow where all subtasks would get canceled when their parent is canceled. I am copying the existing subtask workflows but for some reason I can't get mine to work. I would write something like this but it doesn't work:

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

exports.rule = entities.Issue.onChange({
 title: 'Cancel child when parent is canceled',
 guard: (ctx) => {
   return ctx.issue.isChanged("State");
   
 },
 action: (ctx) => {
   const issue = ctx.issue;
   const cancelChild = function (subtask) {
     subtask.fields.State = "Canceled";
   };
   issue.links["subtask of"].forEach(cancelChild);
 },
 requirements: {
   Subtask: {
     type: entities.IssueLinkPrototype,
     name: 'Subtask',
     outward: 'parent for',
     inward: 'subtask of'
   }
 }
});
 

 

Do you have any advice for me?

0
2 comments

Hi! I see several issues with this workflow code: the conditions in the guard function are not sufficient; neither the State field nor the “Canceled” state value are set in the requirements section; “subtask of” needs to be replaced with “parent for”. Please find a revised version below which should address your needs:

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

exports.rule = entities.Issue.onChange({
title: 'Cancel child when parent is canceled',
guard: (ctx) => {
  return ctx.issue.isChanged("State") && ctx.issue.links["parent for"] && ctx.issue.becomes("State", "Canceled");
  
},
action: (ctx) => {
  const issue = ctx.issue;
  const cancelChild = function (subtask) {
    subtask.fields.State = ctx.State.Canceled;
  };
  issue.links["parent for"].forEach(cancelChild);
},
requirements: {
  Subtask: {
    type: entities.IssueLinkPrototype,
    name: 'Subtask',
    outward: 'parent for',
    inward: 'subtask of'
  },
  State: {
    type: entities.State.fieldType,
    Canceled: {}
  }
}
});

Feel free to refer to our Developer Portal for more information on workflows API, as well as general advice on composing workflows.

1

Thank you for your help, it works correctly now.

0

Please sign in to leave a comment.