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

Stephen Crandell February 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

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Stephen Crandell February 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 February 27, 2015

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

JamieA
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.
March 1, 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 March 4, 2015

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

JamieA
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.
March 4, 2015

perfect, thanks ;-)

1 vote
Stephen Crandell February 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
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.
February 27, 2015

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

Stephen Crandell February 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 February 27, 2015

searching for the right syntax to initialize the componentManager

JamieA
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.
February 27, 2015

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

0 votes
JamieA
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.
February 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.

0 votes
JamieA
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.
February 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.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events