Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

how do I link a field of pickers with multiple issues to tasks?

Evgeniy April 16, 2021

Good afternoon. It is not possible to implement a script that, when creating a request on a custom portal, creates a certain relationship based on the completed tasks from the multiple issue pikers field.

 

The result of what I tried, alas, does not work.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.Issue

final def linkName = "мероприятия"

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)

def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(14020)
def linkType = issueLinkTypeManager.issueLinkTypes.find{ it.name == linkName }
def story = issue.getCustomFieldValue(customField) as Issue

if (story) {
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
ComponentAccessor.issueLinkManager.createIssueLink(story.id, issue.id, linkType.id, 0L, user)
}

1 answer

1 accepted

0 votes
Answer accepted
Max Lim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 18, 2021

Your request is not exactly clear but from what I read on your script. You are probably using a post function on a create transition.

1. Have you moved your post function order to be after "Creates the issue originally."?

2. You might want to use doAfterCreate {} closure too if you are using "clone an issue, and links". Try:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.Issue

final def linkName = "мероприятия"

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)

def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(14020)
def linkType = issueLinkTypeManager.issueLinkTypes.find{ it.name == linkName }
def story = issue.getCustomFieldValue(customField) as Issue

doAfterCreate = {
if (story) {
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
ComponentAccessor.issueLinkManager.createIssueLink(story.id, issue.id, linkType.id, 0L, user)
}
}

 

Evgeniy April 18, 2021

I beg your pardon for the translator.

 

1) I want to specify tasks when creating a request on the portal.

The multiple issue pikers field helped me with this.

2) At the creation stage, I need the tasks in this field (all) to be associated with the task being created. (with the link name I need)

 

At the moment, the script from my example does not work. I can’t tell you what the problem is. I would be very grateful if you can help.

I am a beginner in script runner.

Max Lim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 18, 2021

1. Since you are using a multiple issue picker field, the return type of the field is a list of Issue objects, so please try:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.Issue

final def linkName = "мероприятия"

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)

def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(14020)
def linkType = issueLinkTypeManager.issueLinkTypes.find{ it.name == linkName }
def stories = issue.getCustomFieldValue(customField) as List<Issue>

if (stories) {
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
stories.each {
ComponentAccessor.issueLinkManager.createIssueLink(it.id, issue.id, linkType.id, 0L, user)
}
}

 

2. Can you confirm that you have moved your post function to be after "Creates the issue originally."?

post function orders.png

 

3. If above doesn't work, please provide more details:

  • Are you using "Custom script post-function"? or other ScriptRunner's built-in post-functions (end with "[ScriptRunner]"):
    list of post functions.png

  • Can you provide screenshots of your configurations?
Like Evgeniy likes this
Evgeniy April 19, 2021

Thank you very much! Everything worked out.

Tell me please. How can I delete the value in the picker field with multiple issues after filling in the links?

Evgeniy April 19, 2021

One more thing, in the script we choose the Name of the connection, and if I need to specify a specific outgoing or incoming connection ?

Max Lim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 19, 2021

Tell me please. How can I delete the value in the picker field with multiple issues after filling in the links?

You can set the field to null and update the database like so:

issue.setCustomFieldValue(customField, null)
ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, true)

 

In the script we choose the Name of the connection, and if I need to specify a specific outgoing or incoming connection ?

You can specify the link as outward or inward by changing the order of arguments, like so:

ComponentAccessor.issueLinkManager.createIssueLink(it.id, issue.id, linkType.id, 0L, user)
// or
ComponentAccessor.issueLinkManager.createIssueLink(issue.id, it.id, linkType.id, 0L, user)

 I hope this helps!

Like Evgeniy likes this
Evgeniy April 19, 2021

Added the code to the end. Returns an error when resetting values.

Evgeniy April 19, 2021

Безымянный.png

Max Lim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 19, 2021

I am sorry, you need to import the appropriate class:

import com.atlassian.jira.event.type.EventDispatchOption

issue.setCustomFieldValue(customField, null)
ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, true)
Evgeniy April 20, 2021

Thank you very much! Everything is working.
Special thanks for your patience and explanation.

I really appreciate it.

Max Lim _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 20, 2021

You are most welcomed. Please accept my answer for future reference.

Suggest an answer

Log in or Sign up to answer