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

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

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++
	}
}

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

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

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

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++
}
}

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

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

searching for the right syntax to initialize the componentManager

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

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.

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
Community showcase
Posted yesterday in United States

Confluence Security Advisory

Good morning Members, Not sure if you are aware. Please read the following: https://www.icloud.com/keynote/0RyJ4VEdNUpjDpGfMhfZx9fEg#Confluence_Security_Advisor More details: https://co...

35 views 1 0
View post

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you