I have a custom field value, which is a single list where multi select is possible.
The user can input this value in a parent issue. Now I want to create a sub task to the parent issue,
therefore the value from the parent issue should be already selected on the create screen of the subtask issue.
I have created following behaviour script, which I added a field mapping to the custom field.
Has anybody a hint, what the problem could be?
import com.atlassian.jira.component.ComponentAccessor import com.onresolve.jira.groovy.user.FieldBehaviours import com.onresolve.jira.groovy.user.FormField import groovy.transform.BaseScript import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.CustomFieldManager @BaseScript FieldBehaviours fieldBehaviours CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() 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) CustomField cfArch = customFieldManager.getCustomFieldObjectByName( "Architecture" ); def parentFieldValue = parentIssue.getCustomFieldValue(cfArch); getFieldByName("Architecture").setFormValue(parentFieldValue)
Community moderators have prevented the ability to post new answers.
Has anybody a hint, what the problem could be?
For a start tell us what happens, I'm going to guess it doesn't set it?
What type of field is Architecture?
If it's a select list https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/subtask-default-fields.html shows how to set select list values.
You are right, sorry, yes the default value will not be selected in the screen.
The field "Architecture" is a single list, where multi select is allowed.
Does I understand it correctly, with this code every custom field will be copied?
List copyCustomFields = [] List<CustomField> parentFields = customFieldManager.getCustomFieldObjects(parentIssue) parentFields.each { CustomField cf -> if (copyCustomFields && !copyCustomFields.contains(cf.name)) { return } Object parentValue = cf.getValue(parentIssue) if (!parentValue) { return } getFieldById(cf.id).setFormValue(parentValue) log.debug("parentValue: ${parentValue?.class} for type ${cf.name}") if (parentValue instanceof Timestamp) { getFieldById(cf.id).setFormValue(outlookDate.formatDMY(parentValue)) } else if (parentValue instanceof Option) { getFieldById(cf.id).setFormValue(parentValue.optionId) } else if (parentValue instanceof List && parentValue[0] instanceof Option) { getFieldById(cf.id).setFormValue(parentValue*.optionId) } else { getFieldById(cf.id).setFormValue(parentValue) } }
PS: The new "hidden fade away visual" for the code snippets sucks, especially when you want to copy paste the code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jamie Echlin [Adaptavist]
I get an error in your script that "TimeStamp" & "Option" is not found.
What includes do I need for these?
You example script has an "import ...", which I cannot resolve. Also copy paste is no easily possible anymore withing your example script.
Remark, that the hardest task in writing script runner plugins, is to knew what class should I use and which is import is necessary for it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For the example code in the documentation if you click on the import statement then it will expand to show you the imports. In the top right corner of the example code theres a "copy to clipboard" button which will allow you to paste the full code somewhere.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Adam Markham [Adaptavist]
Maybe it is because I don't have flash installed, But clicking on import doesn't do anything.
The click on copy to clipboard don't do anything either.
I have FireFox 46.
Please bring back the raw view of the script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sorry, your right this does not work on Firefox. I have raised this as a bug here: https://productsupport.adaptavist.com/browse/SRJIRA-1938
The code is the following, or you can try accessing it on Chrome. Hope this helps.
package com.onresolve.jira.groovy.test.behaviours.scripts 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.getPriorityObject().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) // IF YOU DON'T WANT CUSTOM FIELDS COPIED REMOVE EVERYTHING BELOW HERE // IF YOU ONLY WANT SOME FIELDS INHERITED ADD THEM TO THE LIST BELOW, OR LEAVE EMPTY FOR ALL // eg ['Name of first custom field', 'Name of second custom field'] List copyCustomFields = [] List<CustomField> parentFields = customFieldManager.getCustomFieldObjects(parentIssue) parentFields.each { CustomField cf -> if (copyCustomFields && !copyCustomFields.contains(cf.name)) { return } Object parentValue = cf.getValue(parentIssue) if (!parentValue) { return } getFieldById(cf.id).setFormValue(parentValue) log.debug("parentValue: ${parentValue?.class} for type ${cf.name}") if (parentValue instanceof Timestamp) { getFieldById(cf.id).setFormValue(outlookDate.formatDMY(parentValue)) } else if (parentValue instanceof Option) { getFieldById(cf.id).setFormValue(parentValue.optionId) } else if (parentValue instanceof List && parentValue[0] instanceof Option) { getFieldById(cf.id).setFormValue(parentValue*.optionId) } else { getFieldById(cf.id).setFormValue(parentValue) } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Adam Markham [Adaptavist]
I got an error message because of "package com.onresolve.jira.groovy.test.behaviours.scripts"
and an error inside the script:
Can you help?
script_runner_error.png
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok, that script is for if you have it as an external script. It looks like you are adding it inline.
In that case you need to remove the package declaration and possibly the following line also as well as the BaseScript import line.
@BaseScript FieldBehaviours fieldBehaviours
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Adam, can I bother you with a related question? I've tried to use the suggested script at https://scriptrunner.adaptavist.com/latest/jira/recipes/behaviours/subtask-default-fields.html for Subtask defaulting. However, the script is not compiling, the groovy interpreter complaining on static type checking problem for these code snippets: "parentValue[0] instanceof Option" and "parentValue*.optionId". It appears that Jira/SR have changed something over last year or so and no longer support default type casting from Object to List. Would you know what needs to be done to make this code legit again?
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.