Forums

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

ScriptRunner Console: WorkflowException error when cloning issue

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 31, 2023

I have this script that I run as needed on Console and it used to work without fail. It would grab issues from a JQL and bulk clone those issues. Now that I have Data Center and the ScriptRunner version 8.8.0, I get WorkflowException error. It told me to check if the plugin is compatible, which it is. I even upgrade the plugin to 8.11.0, and still get the same error.

I would get the error: com.atlassian.jira.exception.CreateException [...blah...] WorkflorException: Error occurred while creating issue.

I check my workflow and there are no properties or any validators/conditions/post-functions that could block this script:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.issueManager
import com.atlassian.jira.bc. issue.search.SearchService
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.jql.parser.JqlQueryParser

//defining key variables overall
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def changeHolder = new DefaultIssueChangeHolder()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueFactory = ComponentAccessor.getIssueFactory()
def searchService = ComponentAccessor.getComponent(SearchService)
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)

//search and parse through query
def query = "issuekey = TEST-1"
def parsedQuery = jqlQueryParser.parseQuery(query)
PagerFilter pageFilter = PagerFilter.unlimitedFilter
List<Issue> queryResults = searchService.search(currentUser,parsedQuery,pageFilter).results

//make each issue in results mutable
queryResults.each { Issue it ->
    MutableIssue mutableIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(it.key)

    //set clone issue type and clear other fields
    def toClone = issueFactory.cloneIssueWithAllFields(mutableIssue)
    def issueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(mutableIssue.projectObject).find{it.name=="Test"}
    toClone.setIssueTypeObject(issueType)
    toClone.setCreated(new Date().toTimestamp())
    toClone.setAssignee(null)
    toClone.setFixVersions(null)

    def cf = customFieldManager.getCustomFieldObjects(mutableIssue).find{it.name=="Custom Field"}
    def cfConfig = cf.getRelevantConfig(mutableIssue)
    def cfValue = mutableIssue.getCustomFieldValue(cf)
    toClone.setCustomFieldValue(cf, null)

    //logs to check old issue
    log.warn("Old Issue: " + toClone.key + "-" + toClone.summary + "||Custom Field: " + toClone.getCustomFieldValue(cf))

    //create the clone issue
    def cloned = issueManager.createIssueObject(currentUser, toClone)//here is the where the error highlights
    def newIssue = issueManager.getIssueObject(cloned.key) as MutableIssue

    //set values to new cloned issue like epic links
    def epicField = customFieldManager.getCustomFieldObjectsByName("Epic Link").getAt(0)
    def newEpic = issueManager.getIssueObject("TEST-20")
    epic.updateValue(null, newIssue, new ModifiedValue(newIssue.getCustomFieldValue(epicField), newEpic), changeHolder)

    log.warn("New Issue: " + newIssue.key + "-" + newIssue.summary + "||Custom Field: " + newIssue.getCustomFieldValue(cf) + "||Epic Link: " + newIssue)
}

 

Why all of a sudden it wouldn't work when before this script use to clone perfectly fine? I made no changes other than the query.

1 answer

1 accepted

0 votes
Answer accepted
Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 31, 2023

Resolved.

It turns out if you don't have your workflow mapped to the specific issue type you are cloning, meaning under the Workflow Schemes if the workflow is assigned as "Unassigned Types", then it will error out. 

After mapping it to a specific issue type then the above script works.

Suggest an answer

Log in or Sign up to answer