duplicate task based on multiple selection in custom field

kerensho February 9, 2021

Hi All,

I'm taking my first steps in script runner for jira, and though I've read (and tried) a lot questions & answers  here , I'm still facing issues, so here goes:

I need to create a few tasks based on values of multi selection customized field, here are the details:

I have a custom field  " Office" from type multi-selection list, let's say OfficeA, OfficeB, OfficeC.

When a task is created I need to create separate tasks,  according to the selected values in the "Office" field. Each  of those tasks must have the same field values as the original one, but Office" which needs to be marked with a single value which was selected.

For example: if selected values are OfficeA, OfficeB the result will be 2 (almost) identical tasks, the only difference is that one of them will be Office=OfficeA and the other Office = OfficeB.

 

After the creation of the additional tasks - I don't care about the original task, if it helps I can use it as one of the "new" tasks (with removal of redundant "Office" values)

No need in coping comments, links, etc. only the fields.

 

We use Jira server 8.5.6 , and script runner 6.16.0, I added a post function in the "create" transition which uses the "clone an issue, and links", currently I have in condition 'OfficeA' in cfValues['Office']. Value, but I don't know how to fill the "Office" value for the cloned task since it will also clone another task in its create transition L

 

A few question here:

  • Is there a way to clone all required tasks us a single post functions or should I add post function per optional value of "Office"?
  • How do I avoid the internal loop of  create transition (in other words – how to write the condition such it will duplicate only the original task?)
  • I also wanted to assign specific user for every "Office" value, I tried to use issue.setAssigneeId('OfficeA_user') but that didn't work , I understood that there was an issue with the set function and I should use the update but couldn't understand hoe to use it (nor the difference between them)

 

 

Any assistance will be appreciated J

Thanks

Keren

 

1 answer

0 votes
scott_boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 9, 2021

@kerensho 

I actually just did something similar, rather than a post function, I did it as a listener, as I need it to create tasks every time the checkbox field is updated. I would suggest that rather than using the same issue type, you make the new tasks subtasks of the original task (which is what I did). I have an issue type called Application Change Request and subtasks called Deployment Tasks. I have a field called Deployment Profiles that when updated is read and a Deployment Task created for each option selected. Below is my script for reference, it might help you in what you're trying to accomplish:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.FieldManager
import com.atlassian.jira.issue.fields.screen.FieldScreenManager
import com.atlassian.jira.issue.fields.screen.FieldScreenLayoutItem
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.version.Version
import java.sql.Timestamp

def im = ComponentAccessor.getIssueManager()
//def parentIssue = im.getIssueObject("DIGCLM-91") //for debugging
MutableIssue parentIssue = event.issue as MutableIssue

if (parentIssue.getIssueType().name == "Application Change Request" && event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Deployment Profile(s)"})
{
def cfm = ComponentAccessor.getCustomFieldManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def stm = ComponentAccessor.getSubTaskManager()
def cm = ComponentAccessor.getConstantsManager()
def fm = ComponentAccessor.getFieldManager()
def fsm = ComponentAccessor.getFieldScreenManager()

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def deploymentProfiles = cfm.getCustomFieldObjectsByName("Deployment Profile(s)")
def parentDeploymentProfiles = parentIssue.getCustomFieldValue(deploymentProfiles[0]) as List
def subTasks = parentIssue.getSubTaskObjects()

parentDeploymentProfiles.each { profile ->

boolean subtaskExists = false

subTasks.each { subtask ->

if (subtask.getSummary() == "Deployment Profile: " + profile as String)
{
subtaskExists = true
}
}

if (!subtaskExists)
{
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setAssigneeId(parentIssue.assigneeId)
newSubTask.setSummary("Deployment Profile: " + profile as String)
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(parentIssue.getProjectObject())
newSubTask.setFixVersions(parentIssue.getFixVersions())
newSubTask.setReporter(currentUser)

def screen = fsm.getFieldScreen(13002 as Long)
def tab = screen.getTab(0)
def fields = fsm.getFieldScreenLayoutItems(tab)

fields.each { fieldLYOI ->

def field = fm.getField(fieldLYOI.getFieldId())
log.warn(field)
if (fm.isCustomField(field))
{
def cf = fm.getCustomField(fieldLYOI.getFieldId())
newSubTask.setCustomFieldValue(cf, parentIssue.getCustomFieldValue(cf))
}
}

newSubTask.setIssueTypeId(cm.getAllIssueTypeObjects().find{
it.getName() == "Deployment Task"
}.id)

def newIssueParams = ["issue" : newSubTask] as Map<String,Object>
im.createIssueObject(currentUser, newIssueParams)
stm.createSubTaskIssueLink(parentIssue as Issue, newSubTask as Issue, currentUser)
}
}
}
scott_boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 9, 2021

I probably should put more comments in there... lol

Suggest an answer

Log in or Sign up to answer