It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How do I use Script post function to create subtasks based on a checkbox selection set?

Stephen Crandell Feb 26, 2015

I am trying to create subtasks based on checkbox selections made during a transition. I created a screen with the checkboxes custom field callled Subtasks. There are about 10 checkboxes for this field and I want when the user clicks OK on the screen, the post function automatically create subtasks based on the selected checkboxes.

 

Pseudocode:

Collection selectedChBx = get checked checkboxes;

for each selectedChBx 

        if selectedChBx.text = "Development (tech 1)"

                   create subtask with a task summary "Development (tech 1)"

       else if selectedChBx.text = "Documentation (tech 1)"

                   create subtask with a task summary "Documentation (tech 1)"

next

 

Any guidance on how to pull in the selected checkboxes? Here is what I have thus far:

 

cfsubTasks = customFieldManager.getCustomFieldObjectByName("Subtasks")

Collection subTasks= (Collection) issue.getCustomFieldValue(cfsubTasks);

if(subTasks != null)
{
   for(i in 0..subTasks.size()-1)
   {
     String subtaskNeeded = subTasks.get(i).getName();
     if(subtaskNeeded = "Development (tech1)")
     issueObject = issueFactory.getIssue()
     issueObject.setProject(issue.getProject())
     issueObject.setIssueTypeId("10")
     issueObject.setParentId(issue.getId())
     issueObject.setSummary("Development (tech1)")

     subTask = issueManager.createIssue(authenticationContext.getUser(), issueObject)
     subTaskManager.createSubTaskIssueLink(issue.getGenericValue(), subTask, authenticationContext.getUser())
     i++
   }
}

 

 

4 answers

1 accepted

0 votes
Answer accepted
Stephen Crandell Feb 27, 2015

First off, THANK YOU Jamie Echlin!!!!!

Finally got everything working. This will work with any number of choices, I just limited mine to 3 development and 2 documentation.

Final Script:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import java.lang.Boolean;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager() 
def issueFactory = ComponentAccessor.getIssueFactory()
def issueManager = ComponentAccessor.getIssueManager()
def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def subTaskManager = ComponentAccessor.getSubTaskManager()
cfsubTasks = customFieldManager.getCustomFieldObjectByName("Subtasks")
subTasks= (Collection) issue.getCustomFieldValue(cfsubTasks);
if(subTasks != null)
{ 
	for(i in 0..subTasks.size()-1)
	{
		def subtaskNeeded = subTasks.get(i).getValue();
		
		if (subtaskNeeded.subSequence(0,11) == "Development")
		{
			issueObject = issueFactory.getIssue()
			issueObject.setProject(issue.getProject())
			issueObject.setIssueTypeId("56") 
			issueObject.setParentId(issue.getId())
			if (subtaskNeeded == "Development (J2EE)")
			{ 	
				issueObject.setSummary("Development (J2EE)")
			}
			else if (subtaskNeeded == "Development (C++)")
			{	
				issueObject.setSummary("Development (C++)")
			}
			else if (subtaskNeeded == "Development (Database Activity)")
			{	
				issueObject.setSummary("Development (Database Activity)")
			}
		}
		if (subtaskNeeded.subSequence(0,13) == "Documentation")
		{ 
			issueObject = issueFactory.getIssue()
			issueObject.setProject(issue.getProject())
			issueObject.setIssueTypeId("57") 
			issueObject.setParentId(issue.getId())
			if (subtaskNeeded == "Documentation (Technical)")
			{
				issueObject.setSummary("Documentation (Technical)")
			}
			else if (subtaskNeeded == "Documentation (Functional)")
			{
				issueObject.setSummary("Documentation (Functional)")
			}
		}
		subTask = issueManager.createIssue(authenticationContext.getLoggedInUser(), issueObject)
		subTaskManager.createSubTaskIssueLink(issue.getGenericValue(), subTask, authenticationContext.getLoggedInUser())
		i++
	}
}
Stephen Crandell Feb 27, 2015

I am going to switch all the if statements to a switch statement

JamieA Mar 01, 2015

No problem. Would probably be more helpful to others if you put that in the {code} macro, which would preserve the formatting.

Stephen Crandell Mar 04, 2015

updated with code block. Thanks again Jamie!!!!

JamieA Mar 04, 2015

perfect, thanks ;-)

1 vote
Stephen Crandell Feb 27, 2015

Ok, I am close. I am now getting the following error, any ideas? I have called the CustomFieldManager.

[scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: TEST-12345, actionId: 21, file: <inline script>
groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script203
at Script203.run(Script203.groovy:11)

 

here is my code:

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import java.lang.Boolean;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;

cfsubTasks = customFieldManager.getCustomFieldObjectByName("Subtasks")

subTasks= (Collection) issue.getCustomFieldValue(cfsubTasks);
log.warn ("subTasks: " + subTasks)
if(subTasks != null)
{
for(i in 0..subTasks.size()-1)
{
def subtaskNeeded = subTasks.get(i).getName();

if (subtaskNeeded.subSequence(0,11) == "Development")
{
issueObject = issueFactory.getIssue()
issueObject.setProject(issue.getProject())
issueObject.setIssueTypeId("56")
issueObject.setParentId(issue.getId())

if (cfsubTasks*.value.contains("Development (J2EE)"))
{ issueObject.setSummary("Development (J2EE)")}
else if (cfsubTasks*.value.contains("Development (C++)"))
{ issueObject.setSummary("Development (C++)")}
else if (cfsubTasks*.value.contains("Development (Database Activity)"))
{ issueObject.setSummary("Development (Database Activity)")}
}

if (subtaskNeeded.subSequence(0,13) == "Documentation")
{
issueObject = issueFactory.getIssue()
issueObject.setProject(issue.getProject())
issueObject.setIssueTypeId("57")
issueObject.setParentId(issue.getId())

if (cfsubTasks*.value.contains("Documentation (Technical)"))
{ issueObject.setSummary("Documentation (Technical)")}
else if (cfsubTasks*.value.contains("Documentation (Functional)"))
{ issueObject.setSummary("Documentation (Functional)")}
}

subTask = issueManager.createIssue(authenticationContext.getUser(), issueObject)
subTaskManager.createSubTaskIssueLink(issue.getGenericValue(), subTask, authenticationContext.getUser())
i++
}
}

JamieA Feb 27, 2015

import com.atlassian.jira.component.ComponentAccessor def customFieldManager = ComponentAccessor.getCustomFieldManager()

Stephen Crandell Feb 27, 2015

I added that and now I get the field values but, I got an error for: No such property: issueFactory for class so I added issueFactory = componentManager.getIssueFactory(). Now I get this error: No such property: componentManager for class

Stephen Crandell Feb 27, 2015

searching for the right syntax to initialize the componentManager

JamieA Feb 27, 2015

def issueFactory = ComponentAccessor.getIssueFactory() don't use component manager, use the static methods on CompAcc.

0 votes
JamieA Feb 26, 2015

Looks like you are mostly on the right track, give or take. Add some logging to your code so you can see where you are getting to then check the application log, eg

log.warn ("subTasks: " + subTasks)

etc. You can change the log level to debug later.

0 votes
JamieA Feb 27, 2015

At Stephen, I've given you two upvotes so that should give you enough to comment. use: authenticationContext.getLoggedInUser() instead of what you're using.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

1,096 views 13 15
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you