Hi
I need to create several subtasks when creating a issue.
The number of subtasks depends on the filled in custom fields when creating a issue.
The user in the custom field must become a performer in subtasks
I saw in the scriptranner the possibility of creating subtasks, but it is difficult for me to figure out how to do what I described above.
pls Help)
You certainly will want an addon. Scriptrunner, Powerscripts, Automation for Jira all are good. I currently use the last one in this list and it is pretty easy to use. It is less "scripty". Bob Swift is coming out with one that focuses on "drag and drop" automation so that may be worth a look as well. It is best to assess you needs now and in the future, read the marketplace overviews and ultimately trial a few.
To add to what Jack said, if you choose scriptrunner or already have it, here is a snippet that creates subtasks. You would have to create a custom script postfunction.
import com.atlassian.jira.component.ComponentAccessor
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def userManager = ComponentAccessor.getUserManager()
def numberOfSubtasksId = 11001
def numberOfSubtasks = customFieldManager.getCustomFieldObject(numberOfSubtasksId)
def numberOfSubtasksValue = issue.getCustomFieldValue(numberOfSubtasks)
def performerId = 11002
def performer = customFieldManager.getCustomFieldObject(performerId)
def performerValue = issue.getCustomFieldValue(performer)
for (int i=1; i<=numberOfSubtasksValue; i++){
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setReporter(issue.reporter)
newSubTask.setAssignee(performerValue?.getKey())
newSubTask.setSummary("Subtask n°" + i)
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)
newSubTask.setDescription("Description for Subtask n°" + i)
Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(issue.reporter, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, issue.reporter)
}
Assuming numberOfSubtasks is a Number field and performer is a User picker (single).
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, we already have scriptrunner.
I have several custom fields "Person 1 - n "(User picker (single)) When a issue is created, it is necessary that subtasks are created depending on the filled "Person" fields and each Person becomes an executor in a subtask.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
How do you know how many subtasks you want to create ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Then you can use this (10001,10002,10003 being the custom field ids).
import com.atlassian.jira.component.ComponentAccessor
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def personFieldsIds = [10001,10002,10003]
for (int personFieldsId:personFieldsIds){
def person = customFieldManager.getCustomFieldObject(personFieldsId)
def personValue = issue.getCustomFieldValue(person)
if (personValue){
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setReporter(issue.reporter)
newSubTask.setAssignee(personValue?.getKey())
newSubTask.setSummary("Subtask for " + personValue?.getDisplayName())
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)
newSubTask.setDescription("Description for " + personValue?.getDisplayName())
Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(issue.reporter, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, issue.reporter)
}
}
Also you could use a multi user field instead of three single ones.
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This script does not work for me. The validator accepted it, but subtasks are not created. What am I doing wrong ? Maybe affairs in the Jira version? My version v6.3.10
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That might be because of the version indeed, I am working on 7.6. Could you please take a look into the logs and paste the error here ?
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
in logs
/secure/QuickCreateIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: null, actionId: 1, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script195.groovy: 14: unable to resolve class MutableIssue
@ line 14, column 14.
MutableIssue newSubTask = issueFactory.getIssue()
^
1 error
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please add
import com.atlassian.jira.issue.MutableIssue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
new error
[workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script8 at Script8.run(Script8.groovy:13)
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.
Hi @Marat , sorry I was out of office.
Please add :
def customFieldManager = ComponentAccessor.getCustomFieldManager()
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please save as is, these warnings appear beacuse we did not specify the class of the object, but it should not be a problem at execution time.
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
new error
2019-05-31 13:06:35,116 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2019-05-31 13:06:35,116 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setAssignee() is applicable for argument types: (java.lang.String) values: [testuser] Possible solutions: setAssignee(com.atlassian.crowd.embedded.api.User), getAssignee(), setAssigneeId(java.lang.String), getAssigneeId() at Script158.run(Script158.groovy:18)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is your field a single user picker ?
Can you add
log.error("personValue : " + personValue)
log.error("personValue class : " + personValue.getClass())
before the setAssignee line and paste the logs again ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
2019-05-31 13:43:54,357 ERROR [workflow.ScriptWorkflowFunction]: personValue : TESTUSER(testuser) 2019-05-31 13:43:54,357 ERROR [workflow.ScriptWorkflowFunction]: personValue class : class com.atlassian.jira.user.DelegatingApplicationUser 2019-05-31 13:43:54,360 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2019-05-31 13:43:54,360 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setAssignee() is applicable for argument types: (java.lang.String) values: [testuser] Possible solutions: setAssignee(com.atlassian.crowd.embedded.api.User), getAssignee(), setAssigneeId(java.lang.String), getAssigneeId() at Script176.run(Script176.groovy:20)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please replace setAssignee line with :
newSubTask.setAssignee(personValue)
or
newSubTask.setAssigneeId(personValue?.getKey())
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
2019-05-31 14:04:45,525 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2019-05-31 14:04:45,525 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> java.lang.IllegalArgumentException: Parent issue cannot have a null ID. at com.atlassian.jira.issue.IssueImpl.setParentObject(IssueImpl.java:1087) at com.atlassian.jira.issue.MutableIssue$setParentObject$6.call(Unknown Source) at Script184.run(Script184.groovy:22)
2019-05-31 13:53:53,608 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2019-05-31 13:53:53,608 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setAssignee() is applicable for argument types: (com.atlassian.jira.user.DelegatingApplicationUser) values: [testuser(testuser)] Possible solutions: setAssignee(com.atlassian.crowd.embedded.api.User), getAssignee(), setAssigneeId(java.lang.String), getAssigneeId() at Script180.run(Script180.groovy:20)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So first option seems to work for Assignee, the error is not the same. Where are you calling this script ? It seems that issue object is null.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The script is written in the post function when creating a issue.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
def constantManager = ComponentAccessor.getConstantsManager()
def issueManager = ComponentAccessor.getIssueManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def subTaskManager = ComponentAccessor.getSubTaskManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def personFieldsIds = [14098,14099]
for (int personFieldsId:personFieldsIds){
def person = customFieldManager.getCustomFieldObject(personFieldsId)
def personValue = issue.getCustomFieldValue(person)
if (personValue){
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setReporter(issue.reporter)
log.error("personValue : " + personValue)
log.error("personValue class : " + personValue.getClass())
newSubTask.setAssigneeId(personValue?.getKey())
newSubTask.setSummary("Subtask for " + personValue?.getDisplayName())
newSubTask.setParentObject(issue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)
newSubTask.setDescription("Description for " + personValue?.getDisplayName())
Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(issue.reporter, newIssueParams)
subTaskManager.createSubTaskIssueLink(issue, newSubTask, issue.reporter)
}
}
ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> java.lang.IllegalArgumentException: Parent issue cannot have a null ID. at com.atlassian.jira.issue.IssueImpl.setParentObject(IssueImpl.java:1087) at com.atlassian.jira.issue.MutableIssue$setParentObject$6.call(Unknown Source) at Script184.run(Script184.groovy:22)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you please provide a screenshot of your transition post functions ? The script post function should be after the "Creates the issue originally.".
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Moved script post function after the "Creates the issue originally." and got a new error))
2019-05-31 16:59:56,316 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: UWORK-43, actionId: 1, file: <inline script> java.lang.NullPointerException: Cannot get property 'id' on null object at Script184.run(Script184.groovy:24)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.