Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

This document guides you on how to get more out of the Create issue post-function such as creating multiple issues one each based on the values of a multi-select field, linking newly created issues together, setting the field values of the newly created issue from the iterator and so on.

...

Using the "Multiple iterator" feature of the Create issue post-function, you can create multiple issues at once during the execution of this post-function, optionally linking them to the current issue. The iterator script is a Groovy expression that must return an array or a collection of values. The post-function will iterate over this array or collection and create one new issue per value. The below sections explain some of the use cases.

...

To create one issue each for the users jdoe,tblack and dcharlie and assign the newly created issues to the respective user

  1. In the Iterator section input

    Code Block
    languagejs
    ["jdoe","tblack","dcharlie"]
  2. Go to Set fields of new issue section

  3. Under Additional fields select "Assignee"

  4. Click on Add

  5. Select Set field value to Groovy expression option from the drop-down

  6. Input this script:

    Code Block
    languagejs
    it

To create one issue each for the users in a multi-user field

  1. In the Iterator section input

    Code Block
    languagejs
    issue.get("Multi-user picker field")
  2. To assign the newly created issues to the respective user:

  3. Go to Set fields of new issue section

  4. Under Additional fields select "Assignee"

  5. Click on Add

  6. Select Set field value to Groovy expression option from the drop-down

  7. Input this template:

    Code Block
    languagejs
    it

...

Code Block
languagejs
[ [assignee:"jdoe", summary:"Issue for John Doe"] ,  [assignee:"tblack", summary:"Issue for Tim Black"] ]

then two new issues will be created. Under the "Set fields of new issue" you can set the Summary of the newly created issue to the result of this Groovy expression:

Code Block
languagejs
it.summary

and Assignee to the result of this Groovy expression:

Code Block
languagejs
it.assignee

...

Info

Clone Story: Add the Create issue post-function to the "Clone now" transition.

  1. Select the destination project from Project

  2. Select the Issue type as "Story"

  3. Select the link type as "is cloned by"

  4. Using the "Set fields of the new section" set the fields on the Story (copy or set with new values)

  5. Select the “Post-creation script” option and add this script:

    Code Block
    transientVars.mainIssueKey = newIssueKey
  6. Save the post-function.

Clone Subtasks: Add another Create issue post-function to the "Clone subtasks" transition after the above one

  1. Select the destination project from Project

  2. Select the Issue Type as Calculated, with value

    Code Block
    it.issuetype.name
  3. Select the Parent issue as “Calculated” and input the following script

    Code Block
    transientVars.mainIssueKey
  4. Input the Summary as:

    Code Block
    it.summary
  5. Select "Multiple issue creation" option

  6. In the iterator write the following script:

    Code Block
    issue.subTaskObjects
  7. Save the post-function.

  8. Move it after the first post-function.

  9. Publish the workflow

Now when you trigger the transition "Clone now" the Story and its subtasks are cloned to the new project.

Link newly created issues

When using the "Multiple issue creation" feature of the Create Issue post-function, you can link the newly created issues together. There are two approaches to this as explained below. Consider you are creating 3 issues and this is your iterator script:

Code Block
languagejs
[
  {
    "summary":"A first issue"
  },
  {
    "summary":"A second issue"
  },
  {
    "summary":"A third issue"
  }
]

Using newIssueKeys variable

This is a very generic approach in which you can use the newIssueKeys global variable that exposes the array of the issue keys of the issues already created by the post-function during previous iterations. For example, if your iterator value is an array of three values (three issues will be created), during the creation of the third issue, newIssueKeys global variable array holds the first two issue keys. This way, you can add in your Iterator objects information about the links to create between issues.

For example

Add linking information to the above array as shown below, where

...

linkTo: represents an index pointing to the issue to be linked to. 0 for the first issue, 1 for the second issue and so on.

...

linkTypeName: represents the name of the link type to be created, like, Blocks

direction: represents the direction to be linked to, like, outward

Code Block
[
  {
    "summary":"A first issue",
  },
  {
    "summary":"A second issue",
    "linkTo": 0,
    "linkTypeName": "Blocks",
    "direction": "inward"
  },
  {
    "summary":"A third issue",
    "linkTo": 1,
    "linkTypeName": "Blocks",
    "direction": "outward"
  }
]

...

Under the "Set fields of new issue" select Linked Issues field

Select "Set field value to" in the drop-down and input the following template:

Code Block
languagejs
[
{% if it.linkTo != null %}
{
  "type": {"name":"{{it.linkTypeName}}"},
  "{{it.direction}}Issue": {"key":"{{newIssueKeys[it.linkTo]}}"}
}
{% endif %}
]

Now when the transition is triggered

  • 3 issues are created, 

  • the second issue is linked to the first one through "is blocked by" link and

  • the third issue is linked to the second by the "blocks" link.

Using the iterator

This is an easier approach in which you can specify the issue links to create in the iterator section., using the following syntax:

...

Add the "Create issue" post-function

...

Select "Multiple issue creation"

Add the following information under the "Iterator"

Code Block
[
  {
    "summary":"A first issue"
  },
  {
    "summary":"A second issue",
    "issueLinks": [
      {"linkName":"blocks","toIssue":1}
    ]
  },
  {
    "summary":"A third issue",
    "issueLinks": [
      {"linkName":"relates to","toIssue":2}
    ]
  }
]

For each value, an issueLinks property can be specified, which contains an array of links to create. Each link is specified by:

  • linkName: Indicates the link direction name as it appears on the issue (e.g. "blocks" or "is blocked by")

  • toIssue: Indicates the issue link to. It contains the 1-based index of the issue to link to; 1 being the first issue created by the iterator, 2 the second, etc.. If 0 is specified, then the new issue is linked to the issue being transitioned.

Prevent issue creation when the transition is run the second time

To avoid creating a second issue when the transition is run a second time you can use the jmwe.last.issue.created (see here for more information) property to check that there is no issue created already by the post-function. In the conditional execution section, input the template:

Code Block
{{ issue | issueProperty("jmwe.last.issue.created") == null }}

This will avoid the creation of an issue by the post-function, the second time the transition is triggered.

Prevent issue creation when the transition is run the second time

When creating a single issue

If you are creating a single issue with the Create Issue(s) post-function, and that new issue is linked to the current issue, you can use the “conditional execution” section with a conditional execution script like:

Code Block
!issue.getLinkedIssues("link direction")

which will prevent the post-function from running if an issue is already linked to the current issue with the same link type direction (e.g. “blocks”).

When creating multiple issues

If you have configured a Create Issue(s) post-function to create multiple sub-tasks based on the selected values of a multi-select / checkboxes field, and you want to make sure that, if the transition is run again, only sub-tasks for values that were selected after the last transition execution are created, you cannot use conditional execution. Instead, you need to modify the Iterator script.

For example, if you iterator script is:

Code Block
issue.get("Checkboxes")

and you have added the “Checkboxes” field to the “Set fields of new issue” section and set it to:

Code Block
it.value

to create one new sub-task per selected value of the Checkboxes field, then you’ll need to update the iterator script to:

Code Block
issue.get("Checkboxes") - issue.subTaskObjects?.collectMany{it.get("Checkboxes")}

which will return the selected Checkboxes minus the Checkboxes values selected on sub-tasks.

The same approach applies if you are creating Stories inside Epics by replacing issue.subTaskObjects with issue.stories. It also works if you are creating issues linked to the current issues through a specific link type, by replacing issue.subTaskObjects with issue.getLinkedIssues(”link type name”).

Prevent loop on the Create transition

...

  1. In the first example, add a condition in the conditional execution template to check that the main issue (issue from which multiples issues are created) is not a subtask. 

    Code Block
    languagejs
    !issue.isSubTask()
  2. In the second example, identify the main issue by its issue type as follows. 

    Code Block
    languagejs
    issue.issuetype.name == "Story"

    The best is to use the jmwe-created-from property of the issue; Using this approach check that the issue is not created from another issue.

    Code Block
    languagejs
    {{ issue | issueProperty("jmwe-created-from") == null }}

Workaround to move an issue and its linked issues

Moving issues to a new project is not supported by the Jira Cloud APIs, so it cannot be implemented by JMWE or any other app. A workaround is to re-create the task and its linked issues in the destination project and delete the main issue and its linked issues. See here how to clone a task and its linked issues

...