Update status of parent when all subtasks completed

How do I correctly identify subtasks and check if they are resolved? 
I am getting an error that I am calling “undefined.every” while trying to update an epic to complete when all its children are completed.

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

exports.rule = entities.Issue.onChange({
 title: 'Set Epic to Done when all subtasks are resolved',
 guard: function(ctx) {
   return ctx.issue.links["subtask of"].isNotEmpty();
 },
 action: function(ctx) {
   var parent = ctx.issue.links["subtask of"].first();

   var allResolved = parent.links["subtasks"].every(function(subtask) {
     return subtask.isResolved;
   });

   if (allResolved && !parent.isResolved) {
     parent.fields.State = ctx.State.Done;
   }
 },
 requirements: {
   State: {
     type: entities.State.fieldType,
     "Done": {}
   }
 }
});

0
2 comments
Official comment

Hi there! A better approach might be using the code of Fix parent task when all subtasks are resolved. This workflow rule contains the necessary logic for setting the required state to the parent whenever all of the subtasks are resolved—you should only need to change “Fixed” to “Done” to get it working.

With regard to your code, parent.links["subtasks"] likely returns undefined because the correct link name is “parent for”, so it should be parent.links["parent for"]. This returns a “Set” type object that you can apply the visitor function to using forEach() instead of  every(). You might also want to introduce more conditions in the guard to avoid the action being executed on every change to a subtask (e.g.: return ctx.issue.becomesResolved && ctx.issue.links["subtask of"].isNotEmpty();).
 

I had not seen that script there! Thank you for linking to it since that does indeed do what I wanted.
Now I can learn from that example rather than my trial by fire :D

Thank you as well for the feedback on the Guard needing more specificity, I will keep that in mind for later
(as well as the other comments on the code I have posted!)

0

Please sign in to leave a comment.