Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,299,126
Community Members
 
Community Events
165
Community Groups

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

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 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.

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 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

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 May 24, 2022

Have a look at this post.

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 May 24, 2022

Yayyy, and you're welcome

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
Community showcase
Published in Jira Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,931 views 37 49
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you