Create Issues Based on Form Field Values
Purpose | Hosting | Compatible Tools |
---|---|---|
Create a new issue based on the values of a choice question (checkbox, dropdown or multi-select dropdown) in a form. | Server or Data Center | Any scripting tool, such as JMWE, Power Scripts or Scriptrunner, that uses Groovy. |
Script | import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.properties.IssuePropertyService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.entity.property.EntityPropertyService
import org.apache.commons.lang3.StringUtils
////If used in a transition, comment next line:
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ISSUE-1")
String formName = "Form Name"
String fieldName = "Issue Creation Dropdown Field"
ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
IssuePropertyService issuePropertyService = ComponentAccessor.getComponentOfType(IssuePropertyService.class)
IssueManager issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
Closure createIssue = { String projectKey, String summary, String priorityName, String issueTypeName ->
def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
def priority = constantsManager.priorities.findByName(priorityName)
def issueInputParameters = issueService.newIssueInputParameters().with {
setProjectId(project.id)
setIssueTypeId(issueType.id)
setReporterId(loggedInUser.getUsername())
setSummary(summary)
setPriorityId(priority.id)
}
def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
if(validationResult.isValid()){
def issueResult = issueService.create(loggedInUser, validationResult)
} else {
log.error("Error while creating issue from ProForma Form: " + validationResult.errorCollection)
}
}
Closure<Long> getFormId = { def property, String name ->
def slurper = new groovy.json.JsonSlurper()
def parsedJson = slurper.parseText(property.value)
String id
parsedJson.forms.each(){ form ->
if(form.name.equals(name)){
id = form.id.toString()
return
}
}
if(id){
return Long.valueOf(id)
}
}
Closure<String> getFieldId = { def property, String field ->
def slurper = new groovy.json.JsonSlurper()
def parsedJson = slurper.parseText(property.value)
for (def question : parsedJson.design.questions){
if(field.equals(question.value.label)){
return question.key
}
}
}
Closure getCheckboxFieldValues = { def property, String field ->
def slurper = new groovy.json.JsonSlurper()
def parsedJson = slurper.parseText(property.value)
for (def answer : parsedJson.state.answers){
String fieldId = getFieldId(property, field)
if(fieldId.equals(answer.key)){
return answer.value.choices
}
}
}
Closure getCheckboxFieldInfo = { def property, String field ->
def slurper = new groovy.json.JsonSlurper()
def parsedJson = slurper.parseText(property.value)
for (def question : parsedJson.design.questions){
if(field.equals(question.value.label)){
return question.value.choices
}
}
}
def allProperties = issuePropertyService.getProperties(loggedInUser, issue.id)
def formId
for(def property : allProperties){
if(property.key.equals("proforma.forms")){
formId = getFormId(property, formName)
}
}
for(def property : allProperties){
if(property.key.equals("proforma.forms" + ".i" + formId)){
def choiceValues = getCheckboxFieldValues(property, fieldName)
def fieldInfo = getCheckboxFieldInfo(property, fieldName)
for (def info : fieldInfo){
if(info.label.equals("Story") && choiceValues.contains(info.id)){
createIssue("PS", "Summary for Story", "Medium", "Change")
} else if(info.label.equals("Problem") && choiceValues.contains(info.id)){
createIssue("PS", "Summary for Problem", "Medium", "Problem")
} else if(info.label.equals("Task") && choiceValues.contains(info.id)){
createIssue("PS", "Summary for Task", "Medium", "Task")
}
}
}
} |
Installation instructions |
|
Possible use cases | Use this script to create new issues based on the value selected on a form. For example, a dropdown menu in a support request form could be used to make new Bug or Feature Request issues. |
Limitations |
|