Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

Code Block
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

  • If used in a transition:

    • Comment line 12

    • Change formName in line 13 to the name of the form that has the choice field used in the script.

    • Change fieldName in line 14 to the name/label of the choice field that has the options used for issue creation.

    • Change value inside info.label.equals() in lines 101, 103 and 105 to the choice values in the field used in the script. Values must be in quotation marks.

    • Change parameters inside createIssue() in lines 102, 104 and 106 to:

      • Project key (in quotation marks)

      • Issue summary (in quotation marks)

      • Issue priority (must match a priority available in the destination and be in quotation marks)

      • Issue Type (must match an issue type available in the destination project and be in quotation marks)

    • More else if clauses can be added if more options exist in the field. The pattern must be the same as lines 103 to 105. else if's can also be removed if fewer options are needed.

  • If used in the script console:

    • Change issue key in line 12 to the issue that has the form with the field used in the script.

    • Change formName in line 13 to the name of the form that has the choice field used in the script.

    • Change fieldName in line 14 to the name/label of the field that has the choice options used for issue creation.

    • Change value inside info.label.equals() in lines 101, 103 and 105 to the choice values in the field used in the script. Values must be in quotation marks.

    • Change parameters inside createIssue() in lines 102, 104 and 106 to:

      • Project key (in quotation marks)

      • Issue summary (in quotation marks)

      • Issue priority (must match a priority available in the destination and be in quotation marks)

      • Issue Type (must match an issue type available in the destination project and be in quotation marks)

    • More else if clauses can be added if more options exist in the field. The pattern must be the same as lines 103 to 105. else if's can also be removed if fewer options are needed.

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

  • All options must be on the same choice field, on the same form.

  • The code between lines 101 and 106 may need to be amended to include other fields that have been set as required for creating issues in your instance.

  • If there are multiple copies of the same form on the issue, the script will run on the last (most recently added) copy of the form.

  • Issues can only be created for Standard issue types (not subtask issue types).

  • This script will may not work if the form is over 32 KB. You can limit the size of your forms by using multiple smaller forms, and by including character/word limits on your text fields.

  • This script will not work on Legacy Forms.