Hi,
I'd like to copy fields from parent to subtask on create screen.
Whenever I create subtask, I want to get the field value from parent.
I've tried to use both listener and script runner, but I've failed.
What more should I do? What more settings should I make?
ref. https://scriptrunner.adaptavist.com/5.4.47/jira/recipes/behaviours/subtask-default-fields.html
I added below script on 'Behaviour Settings > Initialiser' but it's not working.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.web.util.OutlookDate
import com.atlassian.jira.web.util.OutlookDateManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
import java.sql.Timestamp
import static com.atlassian.jira.issue.IssueFieldConstants.*
@BaseScript FieldBehaviours fieldBehaviours
FormField field = getFieldById(getFieldChanged())
FormField parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long
if (!parentIssueId || field.getFormValue()) {
// this is not a subtask, or the field already has data
return
}
def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(parentIssueId)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// REMOVE OR MODIFY THE SETTING OF THESE FIELDS AS NECESSARY
getFieldById(SUMMARY).setFormValue(parentIssue.summary)
getFieldById(PRIORITY).setFormValue(parentIssue.getPriority().id)
//OutlookDate outlookDate = ComponentAccessor.getComponent(OutlookDateManager).getOutlookDate(Locale.getDefault())
//getFieldById(DUE_DATE).setFormValue(outlookDate.formatDMY(parentIssue.getDueDate()))
//getFieldById(COMPONENTS).setFormValue(parentIssue.componentObjects*.id)
//getFieldById(AFFECTED_VERSIONS).setFormValue(parentIssue.affectedVersions*.id)
//getFieldById(FIX_FOR_VERSIONS).setFormValue(parentIssue.fixVersions*.id)
//getFieldById(ASSIGNEE).setFormValue(parentIssue.assigneeId)
//getFieldById(ENVIRONMENT).setFormValue(parentIssue.environment)
getFieldById(DESCRIPTION).setFormValue(parentIssue.description)
//getFieldById(SECURITY).setFormValue(parentIssue.securityLevelId)
//getFieldById(LABELS).setFormValue(parentIssue.labels)
Behaviour is definitely the direction you want to go if you want those fields pre-populated in the create screen.
But you missed a critical alert in the documentation page you linked:
This script hinges on the parentIssueId being present in the form. As such, you will need to associate the script with a visible field. It will not work as an initialiser.
So try to associate the script with the summary field for example.
Note that if you edit the sub-task after it's been created and then clear the summary field, then all the defaults from the parent will be re-applied.
You can fix this by changing line 21-24 to:
if (!parentIssueId || underlyingIssue) {
// this is not a subtask, or the issue has already been created and we don't need default values
return
}
Probably not from a behaviour implementation since the interactions are mostly browser-side or client-side. There are samples out there for how to copy attachments from server side implementations (e.g. in a workflow post-function)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I saw your comment on this post and my issue is related to this and i used below script and it working perfectly coping summary field from parent to sub:task but here i need to add default value before summary (Tested:) and copied the summary from parent please help me with where to add script in the below script
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.web.util.OutlookDate
import com.atlassian.jira.web.util.OutlookDateManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
import java.sql.Timestamp
import static com.atlassian.jira.issue.IssueFieldConstants.*
@BaseScript FieldBehaviours fieldBehaviours
FormField field = getFieldById(getFieldChanged())
FormField parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long
if (!parentIssueId || field.getFormValue()) {
// this is not a subtask, or the field already has data
return
}
def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(parentIssueId)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
// REMOVE OR MODIFY THE SETTING OF THESE FIELDS AS NECESSARY
getFieldById(SUMMARY).setFormValue(parentIssue.summary)
Thanks in advance,
Siva.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's quite simple... just use groovy string interpolation:
def newSummary = "Tested: $parentIssue.summary"
getFieldById(SUMMARY).setFormValue(newSummary)
Any string delimited by double quotes can include variables or object values defined from elsewhere in the script. Just prefix it with a $.
If the expression you want to return is more complicated than just single words separated by dots, you have to surround that expression with {}.
E.g
def newSummary = "Tested: ${parentIssue.summary.toUpperCase()}"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the reply it is working once again thank you very much and very appreciated.
Thanks,
Siva.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.