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.
Community moderators have prevented the ability to post new answers.
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.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you please tell how to dump the 'formContents' map to the log Jamie? I can't find it even after googling it...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In your script: log.warn (formContents.inspect()) I can't try this atm otherwise I'd be more help...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
BTW, I'm using Jira 6.4.11 with ScriptRunner 3.1.4
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I updated my answer above.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I guess I'm on my own.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've updated my answer...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.