Forums

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

Creating new issues with scriptrunner from jql

dD
Contributor
October 1, 2021

Hello Community,

using Scriptrunner I want to create a scheduled job, where at a certain date (once a year) a jql is searching for issues from the issuetype A. For every found issue I want to create a new Issue, of the issuetype B, and copy some information like assignee and the values of some customfields. 

I know how to use the jql and get a list of the issues but I dont know how I shall continue... 

Thankful for your help!!

1 answer

1 vote
Nic Brough -Adaptavist-
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 1, 2021

https://library.adaptavist.com/entity/create-a-backdated-issue is a sample of a really basic "create single issue with some data", you should be able to use that inside an iterator (iterating over your list of issues), and adding copied fields from it should be easy (have a rummage around the rest of the library for examples of working with custom fields)

dD
Contributor
October 1, 2021

Ok, thanks for your advice @Nic Brough -Adaptavist- . 

I tried to use this inside a for loop (as iterator), bacause I am very unexperienced I am not sure if I am doing it right.

Is that the way you ment it?

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

// Set log level to INFO
log.setLevel(Level.INFO)

// The JQL query you want to search with
final jqlSearch = "issuetype = 'SUD Modul Basic' and 'Sommersemester aktiv?' = aktiv"

// Some components
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)

// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
log.error('Invalid query')
return null
}

try {
// Perform the query to get the issues
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
issues.each {
log.info(it.key)
}

// ITERATOR --> create new isse
for (i in issues){
def project = ComponentAccessor.projectManager.getProjectObjByKey("SUD")
if (project){
MutableIssue issue = ComponentAccessor.issueFactory.issue
issue.projectObject = project
issue.issueType.name == "SUD Modul"
issue.summary == "SoSe ${now.format("yyyy")} ${issue.Studiengang}-${issue.Modulkennung}"
ComponentAccessor.issueManager.createIssueObject(user, issue)
}
}
} catch (SearchException e) {
e.printStackTrace()
null
}
Nic Brough -Adaptavist-
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 4, 2021

Yes, that's how I would do it.

I'm not 100% sure of your code, I don't do a lot of coding and have to look stuff up all the time when I do, so I can't validate it.  But you've definitely got the structure right, and it does look like it should work.

(I would change one little thing - I'd move the "if project" check way up the script - there's little point in checking if the same project exists for every single create issue call, I'd just end the script before doing anything else)

Suggest an answer

Log in or Sign up to answer