Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.
×Similar to this question, however I don't want to copy fields from parent to subtask. I'm trying to pre-fill fields for the subtasks based on the parent's custom field value.
If parent issue's custom field checkbox = "A" and parent issue is a certain issue type (i.e. Story), when user goes to More > Create a subtask > the create screen should pre-fill the labels with "label_1" and "label_2".
However, I'm not sure if my script or mapping is correct. I have this behaviour script mapped to issue type Subtask and under the field Labels server-side script:
import com.atlassian.jira.issue.fields.LabelsField
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.LABELS
@BaseScript FieldBehaviours fieldBehaviours
def issueManager = ComponentAccessor.getIssueManager()
def parent = getFieldById("parentIssueId")
def parentIssueId = parent.getFormValue() as String
def parentIssue = issueManager.getIssueObject(parentIssueId)
def parentType = parentIssue.getIssueType()
def checkbox = parentIssue.getCustomFieldValue("Checkbox Custom Field")
if(getActionName() in ["Create Issue", "Create"]){
if(checkbox == "A" && parentType in ["Story", "Bug"]){
// getFieldById(LABELS).setLabel("label_1")
// getFieldById(LABELS).setLabel("label_2")
getFieldById(LABELS).setFormValue("label_1")
getFieldById(LABELS).setFormValue("label_2")
}
}
But the subtasks doesn't show the labels. I've tried both .setLabel and .setFormValue
Expected Results:
Actual Results:
Any ideas what I'm doing wrong?
Hi @Diana Gorv ,
As far as I can tell there are three issues with this script:
First, when you get the value of the checkbox customfield, the value returned is a list of option objects. This means when you perform your condition to check to see if it is equal to "A", it will fail.
What we need to do is to create a list of all the values selected. We can do this by changing this by firstly mapping out the checkbox variable as a List<LazyLoadedOption>. So, we can add this import:
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
And then we can change this line:
def checkbox = parentIssue.getCustomFieldValue("Custom Field Checkbox")
def checkbox = parentIssue.getCustomFieldValue("Custom Field Checkbox") as List<LazyLoadedOption>
Which maps the returned value correctly, so we can then, we can get the list of string values using this line:
def checkBoxValues = checkbox*.value
And change the check from
checkbox == "A"
to
"A" in checkBoxValues
The other two are a little more straight forward. "parentType" is an object, but you are trying to find it in an array of Strings, so we need to get the name of the parentType to check, so:
parentType in ["Story", "Bug"]
parentType.name in ["Story", "Bug"]
And when you set the labels, you need to pass an array, so:
getFieldById(LABELS).setFormValue("label_1")
getFieldById(LABELS).setFormValue("label_2")
becomes:
getFieldById(LABELS).setFormValue(['label_1', 'label_2'])
I have created a snippet for you with the full script, you can find this here:
https://bitbucket.org/Adaptavist/workspace/snippets/jqBx4L (you will have to change the custom field name, as yours is different)
Let me know if this helps!
Kind regards,
Bobby
Another note @Diana Gorv ,
You may want to change this script to run in the "Initialiser" script rather than on the 'Labels' custom field.
The script that you have on the 'Labels' custom field will execute when the form is loaded up, and also when anyone interacts with the labels field. This could cause issues if your users try to add more labels, and the script runs, overwriting their changes.
The Initialiser script is designed to only run when the form is loaded up, used for pre-populating values. As this is your goal, the script should probably go in there.
Kind regards,
Bobby
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Bobby Bailey Thanks for the quick response.
It works! I also had to set parent.getFormValue() "as Long" and not "as String" which is what i had.
Also yes, it did work on Initializer rather than on the field's server-side script.
Final script:
import com.atlassian.jira.issue.fields.LabelsField
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import static com.atlassian.jira.issue.IssueFieldConstants.LABELS
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
@BaseScript FieldBehaviours fieldBehaviours
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parent = getFieldById("parentIssueId")
def parentIssueId = parent.getFormValue() as Long
def parentIssue = issueManager.getIssueObject(parentIssueId)
def parentType = parentIssue.getIssueType()
def checkbox = parentIssue.getCustomFieldValue("Checkbox Custom Field") as List<LazyLoadedOption>
def checkboxValues = checkbox*.value
if(getActionName() in ["Create Issue", "Create"]){
log.warn("Action Name: ${getActionName()}")
if("A" in checkboxValues && parentType.name in ["Story", "Bug"]){
log.warn("Parent Issue ID: $parentIssueId")
log.warn("Parent Issue Type: $parentType")
getFieldById(LABELS).setFormValue(['label_1','label_2'])
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You are most welcome. I missed that, great catch!
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.