Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to retrieve parent issue's custom fields in Initializer Function?

THOMAS SEBASTIAN June 15, 2015

Hi, we are trying to retrieve some of the parent issue's custom fields using Initializer function in Behaviour plugin and use it as default values when user is creating sub tasks.  However we can't find a way to 'refer' to the parent issue.  

The problem that we are having is in the 'Create' screen the sub-task issue has not been initialized and we can't use it to trace back to the parent.  

Is there another way to do it?

Thanks in advance.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
JamieA
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.
June 15, 2015

The Issue object corresponding to the issue on the form can be got through "underlyingIssue". If it's a subtask you can get the parent using underlyingIssue.parentObject.

For subtasks that have not yet been created you can get their ID like this:

log.warn "Parent id: " + getFieldById("parentIssueId")?.formValue

but you will need to put that on the script of some visible field, because it doesn't seem to work in an initialiser.

Update: There is a full example here: https://scriptrunner.adaptavist.com/4.2.0.2-SNAPSHOT/jira/recipes/behaviours/subtask-default-fields.html

Note that you should be able to attach this server-side script as an initializer function, but due to a bug you can't, and you should attach it to some field that will be on the subtask, like the summary field.

THOMAS SEBASTIAN June 16, 2015

Thanks for your reply Jamie however underlyingIssue can only be used once the subtask is created. In the 'Create' screen the issue has not been initialized and therefore underlyingIssue is null in this case.

JamieA
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.
June 16, 2015

Sorry, didn't read your question properly. Try dumping the the "formContents" map to the log... it should be in there. If not, I'm not sure.

THOMAS SEBASTIAN June 16, 2015

Can you please tell how to dump the 'formContents' map to the log Jamie? I can't find it even after googling it...

JamieA
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.
June 16, 2015

In your script: log.warn (formContents.inspect()) I can't try this atm otherwise I'd be more help...

Brent Webster
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.
November 11, 2015

On an existing issue: POMR-73, I inspected formContents and it returns ['id':10910, 'action':81] So issueId = formContents["id"] issue = ComponentManager.getInstance().getIssueManager().getIssueObject(issueId as Long) log.warn "<<BW>> Issue's key is " + issue.key generates the log: .... .user.FieldBehaviours] <<BW>> Issue's key is POMR-73 And that's the correct issue key Now I 'Create Sub-task' from POMR-73 and the inspected value is ['issuetype':10200, 'pid':10000, 'action':0] issueId = formContents["pid"] issue = ComponentManager.getInstance().getIssueManager().getIssueObject(issueId as Long) log.warn "<<BW>> Issue's key is " + issue.key generates the log: .... .user.FieldBehaviours] <<BW>> Issue's key is POMR-1 Wrong issue. Help!!!!

Brent Webster
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.
November 11, 2015

BTW, I'm using Jira 6.4.11 with ScriptRunner 3.1.4

THOMAS SEBASTIAN November 11, 2015

Hi Brent, in the first instance you used 'id' and yet on the second one you use 'pid'. These of course refer to different item: 'id' is issue ID while 'pid' is project ID. Your question is probably the same as mine (at the top); you were trying to get the Issue ID of the parent issue of a subtask that is not yet created? AFAIK it is not possible because when you call formContents at the CREATE screen it is referring to the subtask that is not created yet and therefore don't have any issue ID - only issuetype and project ID as displayed above.

JamieA
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.
November 12, 2015

I updated my answer above.

Brent Webster
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.
November 12, 2015

Sorry Jamie but my morning coffee must not be kicking in yet. Do I created a scripted field called "parentIssueId" and have the script return the parent id? What would the script look like? Sorry for the confusion.

JamieA
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.
November 12, 2015

No it's behaviours code... you create a behaviour, add a field, add a server-side script, and use that to get the parent issue ID. Then presumably you look at the parent issue, to set subtask field defaults. I'll write a complete example at some point.

Brent Webster
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.
November 12, 2015

Sorry Jamie but I'm missing something here. Got the behaviour, a field and a server side script but what variable/instance/class am I querying to determine the parent issue since the issue has not been created yet. Where is this reference to the "underlying issue"? Is there a form related variable like formContents that I reference. Your help is greatly appreciated.

Brent Webster
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.
November 14, 2015

I guess I'm on my own.

JamieA
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.
November 15, 2015

I've updated my answer...

Brent Webster
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.
November 15, 2015

Thank you. I created parentIssueId as a custom single line text field then took the supplied script and pasted it as a serverside script for the Summary field as suggested. I now have real prepopulated subtasks!!!!

Brent Webster
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.
November 16, 2015

It's working great. BTW, I have single select version custom fields so I added } else if (parentValue instanceof ArrayList && parentValue[0] instanceof VersionImpl) { getFieldById(cf.id).setFormValue(parentValue[0].getId()) to handle those custom fields

JamieA
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.
November 16, 2015

You don't need a custom field called parentIssueId, in fact I would have thought that would cause a problem having that. parentIssueId is a hidden field in the form jira creates automatically. Glad it's working though.

Charlie Misonne
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 30, 2016

Hi @Jamie Echlin [Adaptavist]

I encountered the bug you are talinkg about today. Any idea if it's logged somewhere? I'm trying to prefill the components field (from the parent) when creating a subtask. This does not work properly when adding the script on the field level because it will reset the values each time the user wants to add a new component

TAGS
AUG Leaders

Atlassian Community Events