While working in JIRA, there will be scenarios where we want to automatically create and assign issues, usually on transitions. Following groovy script can be used to create an issue:
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.ApplicationUser
import org.apache.commons.lang.StringUtils
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.option.*
def projectid = issue.getProjectObject().getId();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueService issueService = ComponentAccessor.getIssueService()
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
issueInputParameters.setProjectId(projectid)
.setIssueTypeId("10301") // issue type ID
.setSummary("This will be the Summary of newly created issue")
.setAssigneeId(user.key)
.setPriorityId("3") // id of Priority
.addCustomFieldValue("customfield_10704", user.username) // user picker field
.addCustomFieldValue("customfield_10705", someId) // custom field with dropdown ID
log.error("Input params: {}", issueInputParameters);
IssueService.CreateValidationResult createValidationResult = issueService.validateCreate(user, issueInputParameters);
log.error("Validation?: {}", createValidationResult.isValid().toString())
log.error("error: " + createValidationResult.getErrorCollection());
if (createValidationResult.isValid())
{
IssueService.IssueResult createResult = issueService.create(user, createValidationResult);
log.error("Creation result?: {}", createResult.isValid().toString())
if (!createResult.isValid()) {
log.error("Something went wrong")
}
} else {
String cause = StringUtils.join(createValidationResult.getErrorCollection().getErrorMessages(), "/")
log.error("cause :" + cause)
}