Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Create issue and assign to existing epic

Michael Venable
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 6, 2023

Hi,

While creating a new issue using scriptrunner, I would like to assign it an existing epic. it seems the issue.setCustomFieldValue(epicLinkCustomField) is not working. any help would be greatly appreciated. 
The code I have is below. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.context.IssueContextImpl
import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import java.text.SimpleDateFormat

def date = new Date()
def sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")

// the project key under which the issue will get created
final projectKey = 'SEC'
// stores status
final statusKey = 'AN QUE'
// the issue type for the new issue
final issueTypeName = 'Task'
// user with that user key will be the reporter of the issue
final reporterKey = 'MV108788'
// the summary of the new issue
final summary = 'test' + date
// the priority of the new issue
final priorityName = 'Low'
// sets description
final description = 'this is a decription'
//epic
final epicIssueKey = 'test'

def issueService = ComponentAccessor.issueService
def constantsManager = ComponentAccessor.constantsManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager)

//finds epic
def epicLinkCustomField = ComponentAccessor.customFieldManager.getCustomFieldObjects().findByName(epicIssueKey)
//finds status key
def status = ComponentAccessor.constantsManager.getStatusByName(statusKey)
//finds project key
def project = ComponentAccessor.projectManager.getProjectObjByKey(projectKey)
assert project : "Could not find project with key $projectKey"
//finds issue types
def issueType = constantsManager.allIssueTypeObjects.findByName(issueTypeName)
assert issueType : "Could not find issue type with name $issueTypeName"
// if we cannot find user with the specified key or this is null, then set as a reporter the logged in user
def reporter = ComponentAccessor.userManager.getUserByKey(reporterKey) ?: loggedInUser
// if we cannot find the priority with the given name or if this is null, then set the default priority
def issueContext = new IssueContextImpl(project, issueType) as IssueContext
// finds priority
def priorityId = constantsManager.priorities.findByName(priorityName)?.id ?: prioritySchemeManager.getDefaultOption(issueContext)

//sets all issue settings
def issueInputParameters = issueService.newIssueInputParameters().with {
setStatusId(statusId) //not working
setProjectId(project.id)
setDescription(description)
setIssueTypeId(issueType.id)
setReporterId(reporter.name)
setSummary(summary)
setPriorityId(priorityId)
issue.setCustomFieldValue(epicLinkCustomField)
}


def validationResult = issueService.validateCreate(loggedInUser, issueInputParameters)
assert validationResult.valid : validationResult.errorCollection

def result = issueService.create(loggedInUser, validationResult)
assert result.valid : result.errorCollection

1 answer

1 vote
Fabio Racobaldo _Catworkx_
Community Champion
January 6, 2023

Hi @Michael Venable ,

welcome to the Atlassian community!

there are some errors in your script.

//finds epic
def epicLinkCustomField = ComponentAccessor.customFieldManager.getCustomFieldObjects().findByName(epicIssueKey)
//finds status key
def status = ComponentAccessor.constantsManager.getStatusByName(statusKey)
  • //finds epic
    def epicLinkCustomField = ComponentAccessor.customFieldManager.getCustomFieldObjects().findByName(epicIssueKey)
    • You need to search for a field "Epic Link" because you need to retrieve that Custom Field first and not the epic itself. 
  • //finds status key
    def status = ComponentAccessor.constantsManager.getStatusByName(statusKey)
    • You can not create an issue in a different status from the first status of your workflow

Hope this helps,

Fabio

Michael Venable
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 9, 2023

Hi, So i updated my script as follows:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.context.IssueContextImpl

// Replace these placeholders with your own values

def projectKey = "SEC"

def reporter = "MV108788"

def assignee = "MV108788"

def description = "This is the description for the new issue."

def summary = "New issue summary"

def priority = "Low"

def issueType = "Task"

def epicKey = "test"

// Get the issue type and priority objects

def issueTypeManager = ComponentAccessor.getIssueTypeManager()

def priorityManager = ComponentAccessor.getPriorityManager()

def issueTypeObject = issueTypeManager.getIssueType(issueType)

def priorityObject = priorityManager.getPriority(priority)

// Set up the input parameters for the new issue

def inputParams = new IssueInputParametersImpl()

inputParams.setProjectKey(projectKey)

inputParams.setReporter(reporter)

inputParams.setAssignee(assignee)

inputParams.setDescription(description)

inputParams.setSummary(summary)

inputParams.setPriority(priorityObject)

inputParams.setIssueType(issueTypeObject)

inputParams.setFieldValue("EpicLink", epicKey) // Replace "customfield_123" with the actual field ID of the epic link field

// Create the new issue

def issueService = ComponentAccessor.getIssueService()

def createValidationResult = issueService.validateCreate(ComponentAccessor.getJiraAuthenticationContext().loggedInUser, inputParams)

if (createValidationResult.isValid()) {

def createResult = issueService.create(ComponentAccessor.getJiraAuthenticationContext().loggedInUser, createValidationResult)

if (!createResult.isValid()) {

throw createResult.errorCollection.getErrorMessages()

}

} else {

throw createValidationResult.errorCollection.getErrorMessages()

}
im now getting the error:

The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script5287.groovy: 22: unable to resolve class IssueInputParametersImpl @ line 22, column 19. def inputParams = new IssueInputParametersImpl() ^ 1 error </pre>.
not sure how to fix this because im importing:
import com.atlassian.jira.issue.context.IssueContextImpl

Suggest an answer

Log in or Sign up to answer