Create subtask with summary from parent

Jo-André Lia November 14, 2016

Hi all.

 

I'm trying to create a behavior that takes the parent summary and display it in the summary field in the create subtask screen. This is so that the user can more easily create subtasks for stories when there is only one subtask. It is then very often just a copy of the parent summary.

I simply cannot get it to work. I've tried to follow what is described here:

https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/subtask-default-fields.html

 

I've followed what it say in the link and put this on the summary field and not in the initializer as the parentID is null then.

 

This is my script:

 

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.onresolve.jira.groovy.user.FieldBehaviours

public class CopySummaryFromParentToSubtask extends FieldBehaviours {

public Object run() {
FormField parent = getFieldById("parentIssueId")
log.warn("CopySummaryFromParentToSubtask parent: " + parent)
Long parentIssueId = parent.getFormValue() as Long

IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue parentIssue = issueManager.getIssueObject(parentIssueId)
log.warn("CopySummaryFromParentToSubtask parentIssue.summary: " + parentIssue.summary)

def summaryField = getFieldById("Summary")
log.warn("CopySummaryFromParentToSubtask summaryField: " + summaryField)
summaryField.setFormValue(parentIssue.summary)
log.warn("CopySummaryFromParentToSubtask summaryField: " + summaryField)
  }
}

 

The Summary field is not updated at all. It's completely blank. I can input values all over the place and still nothing happens in the screen. In the log however it does print out all my debug statements with the values I expect. Everything looks fine when looking at the log.

 

Also I would like to be able to focus on the summary field and have it all selected so that if the user do want to create a different summary they don't have to erase the summary, they can simply start to type. I haven't found any setFocus(true) or setSelected(true) method that is available. Any suggestions for this?

 

All in all maybe behavior isn't the right thing to use for this?

1 answer

0 votes
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 14, 2016

This:

def summaryField = getFieldById("Summary")

should be:

def summaryField = getFieldById("summary")

For the log line immediately after that line, does it not show the summaryField variable as being null?

Jo-André Lia November 15, 2016

Hi Jamie. Thanks for getting back to me so quick.

That's the thing - I actually tried first with "summary", but got null. I think that might have been when I tried this as an initializer. I then got a value back when I used "Summary". I tried again now that I'm not running it as an initializer and I got it to work! So now it does display the parent summary in the create subtask summary.

I then started to look at how I could prevent it from updating it every time I edit the summary field in those cases where we don't want to use the parent summary. I found a very simple solution to it.

I basically add a field to the create subtask screen that I'm not going to use. I n my case it was Component/s. I then go to behaviors and add a new behavior that I map to the subtasks in all my projects. I enter the fields configuration on the behavior and add the component/s field. I add a server side script and point to the script posted earlier in this post that I have put on the HD. The last thing I do is to click hide on the Component/t field in the behavior. Now the script will be ran when the screen opens and the summary will be copied from the parent to the summary in the create subtask screen, but it will never run again while the screen is open. That way it will work as an initializer. Unless there are some side effect that I don't know about - I think this is pretty genius smile

 

So the only thing remaining now is to have the text selected so that it's easy for the user to write something else in the summary if the parent summary doesn't fit. Is there any way to do that - Select the whole text in a field?

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

Not currently, no... sorry.

Suggest an answer

Log in or Sign up to answer