Hi ,
I have a scenario that on the basis of certain values in Parent tasks , sub tasks are generated automatically based on certain conditions . There will be a logic in Post funtion of create issue how many sub tasks need to be generated on the fly and each subtask would store the value or set the value of a custom field of sub task.
Can any one please guide me how to create a sub task in post funtion automatically using groovy and how to set the value of one of sub task's custom fields ?
Community moderators have prevented the ability to post new answers.
Hi,
Code below will give you a hint. This script will create a number of subtasks depending on parent issue's value of a single select lit. Please let me know if you need further assistance. Can I also ask which version of JIRA you use ?
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.customfields.option.LazyLoadedOption Issue parentIssue = issue if (parentIssue.getIssueTypeObject().getName() == 'Sub-task') return def assigneeId def summariesList def customFieldManager = ComponentAccessor.getCustomFieldManager() def user = ComponentAccessor.getJiraAuthenticationContext().getUser() def issueFactory = ComponentAccessor.getIssueFactory() def subTaskManager = ComponentAccessor.getSubTaskManager() def constantManager = ComponentAccessor.getConstantsManager() def issueManager = ComponentAccessor.getIssueManager() def employeeTypeCF = customFieldManager.getCustomFieldObjectByName("A single select list custom field") LazyLoadedOption employeeTypeField =(LazyLoadedOption) parentIssue.getCustomFieldValue(employeeTypeCF) def valueOfEmployeeType = employeeTypeField?.getValue() if (valueOfEmployeeType == 'Option1') { assigneeId = "paul" summariesList = ["Summary 1"] } else if (valueOfEmployeeType == 'Option2') { assigneeId = "admin" summariesList = ["Summary1", "Summary 2"] } else if (valueOfEmployeeType == 'Option3') { assigneeId = "kate" summariesList = ["Summary 1", "Summary 2", "Summary 3"] } else { log.info("Employee Type field value doe not match") return } for (String summary : summariesList) { def cf = customFieldManager.getCustomFieldObjectByName("A customField name") MutableIssue newSubTask = issueFactory.getIssue() newSubTask.setAssigneeId(assigneeId) newSubTask.setSummary(summary) newSubTask.setParentObject(parentIssue) newSubTask.setProjectObject(parentIssue.getProjectObject()) newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{ it.getName() == "Sub-task" }.id) newSubTask.setCustomFieldValue(cf, "A value") // Add any other fields you want for the newly created sub task Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object> issueManager.createIssueObject(user.directoryUser, newIssueParams) subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user.directoryUser) log.info "Issue with summary ${newSubTask.summary} and assignee ${assigneeId} created" }
Kind regards
Thanks a ton Thanos . I am using JIRA version 6.3.9 and have script runner plugin . I know that there is an inbuilt feature to creat sub task on issue creation but was wondering how do i create "Multiple" Subtasks simultaneously on Parent issue creation . Will try your code and see if that helps .. Thanks a lot .. will try and let you know
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I am trying to create sub-tasks based on certain conditions.
When using the script above I am getting errors like 'cannot find matching method'. We are using JIRA 7.3.3 Could you please provide me with a sample of creating subtasks programmatically on JIRA 7.3.3
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you are using Scriptrunner then this is a built in function. See https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html
You will need to add in the extra actions box something similar to this to set the subtask fields.
issue.summary = 'Subtask summary '
You could do the following to make all the created subtasks have the summary - "Subtask of <original parent summary>"
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Summary'}
issue.summary = 'Subtask: ' + issue.getCustomFieldValue(cf)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Phill Fox . Yes i am using Script runner but was wondering if script runner would help me create "Multiple" Sub- tasks simultaneously on the Parent Issue Creation . Basically on certain values\Condition in Parent issue I may have to create 2 subtasks and on certain condition 3 or 4 or 5 subtasks . Reallly appreciate your help
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can create as many subtasks as you like by just adding a new postfunction for each one. Alternatively if you use the script from @Thanos Batagiannis [Adaptavist] you can also build your conditions in to the creation rules. I would probably go with the built in script for any required subtasks and the script for any that require a condition to be checked.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Phill the subtasks need to be created on the fly and I would not know before hand , how many subtasks this parent issue may have . So I doubt if creating new post function every single time would be helpful. I would also prefer built in script but was just looking for guidance as to how to call " Create Subtask" script multiple times dynamically depending on the condition in Parent issue .
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I was not suggesting creating a new post function as that is not going to be helpful, what I was suggesting was you did it for any that are common. I have found it useful to put certain subtask creation on a workflow action which references back to the same status - a little bit of cunning can enable you to put a field on the transition screen which captures the latest subtask detail to be completed and builds the subtask accordingly.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Phill Fox .. This is what we did .. In script runner , "Create Subtask" built in script , I selected my project , And on Event " Issue Created" , i put a condition on the basis of which Subtask should be generated , then i specified the "Target Issue Type " which is my new subtask to be generated and set the value of custom field in subtask using "Addition Issue Actions" . I specified the Subtask action .. This is good .. Now coming back to the same question , How or where do i specify that i need 2 or 3 or 4 or n number of subtasks on the basis of certain values selected in Parent's issue ? Will it be the condition field in built in script or the post function of Parent issue creation . If it the post function , How do I call "Create Subtasks" multiple times based . Can you specify the method to invoke create subtask through post function. .. Thanks a ton
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can achieve it by creating a script (as you showed) for each potential subtask action. Then placing the condition in the condition box for when that particular subtask should be created. As you will have multiple versions of the script you then call each one in the post function of the create transition. Whilst this is a little cumbersome it gives you control over when and how you can call each script. The "neater" but not necessarily easier approach is to write a single script using Groovy and call that in the post function instead. This however requires a lot more knowledge and understanding of scripting than using the built in function.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Phill .. That is what I am planning to do now . Writing a groovy in post function to create multiple subtasks on the basis of a condition by putting it in a loop . Thanks anyways for all the support .. Really appreciate all the help :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Community moderators have prevented the ability to post new answers.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.