How do I create a subtask and link it to an issue I have auto created in a script listener, for Jira

Bal May 23, 2022

Hope someone here can help, I've been banging my head against my desk!

i'm using scriptrunner for jira cloud.

1) I have a script listener which fires when a new version is created.

2) My script successfully creates a new issue (story)

How do I create subtasks and link them to the newly created issue?

I thought about creating a new listener which fires after an issue is created and performs a text search for a value which is set when the new version is created, but that seems clunky.

Thanks.

2 answers

1 accepted

0 votes
Answer accepted
Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2022

Hi @Balhar Bhachu 

You need to capture the issuekey of the Story you are creating then apply that to the create sub-task example found in the Scriptrunner library.  Based on API documentation you should be able to get this on the return from your create. re: 

Class IssueService.IssueResult

 

Hope that points you in the right direction.

0 votes
Bal May 23, 2022

Thanks Craig. Its the trying to get the issue that I am struggling with. How can I do it on a post, when post is only one way?
I'm using this to create the story issue, which works like a charm:

def issueTypeName = "Story"
def taskType = get('/rest/api/2/issuetype')
.asObject(List)
.body
.find { (it as Map).untranslatedName == issueTypeName } as Map

def taskTypeId = taskType.id

post('/rest/api/2/issue')
.header('Content-Type', 'application/json')
.body([
fields: [
project : [
key: projectKey
],
issuetype: [
id: taskTypeId
],
summary : summary,
priority : [
id: priorityId
]
]
])
.asString().body

Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2022

Hi @Balhar Bhachu sorry that first response was based on serer/DC. 

So for a post it'll be in the response.

From this page:

response.JPG

Bal May 24, 2022

once again thanks for taking the time to respond Craig, its greatly appreciated.

 

I've got this which is working and the variable result contains the json as you described with the id. How do I query the result variable to extract the id?

def result = post('/rest/api/2/issue')
.header('Content-Type', 'application/json')
.body([
fields: [
project : [
id: projectDetails.id
],
issuetype: [
id: taskTypeId
],
summary : "Test story ",
]
])
.asString().body

 

Thanks.

Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 24, 2022

Have a look at this post.

Bal May 24, 2022

sussed it cheers.

.asObject(Map) instead of the asString

and then 

result.body.key

 

worked brilliantly. 

Craig, I thank you good sir for your help.

Like Craig Nodwell likes this
Craig Nodwell
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 24, 2022

Yayyy, and you're welcome

Suggest an answer

Log in or Sign up to answer