Hello,
We have the following script listener set up for one of our projects:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
// a map with select list option to Component
def componentsMap = [
"DOORS": ["DOORS"],
"Rhapsody / Design Manager": ["Rhapsody"],
"CS - Rational Change": ["Change (CS)"],
"CM - Rational Synergy": ["Synergy (CM)"],
"JIRA": ["JIRA"],
"Git/Gerrit": ["Gerrit", "Git"],
"Collaborator": ["Collaborator"],
"Klocwork": ["Klocwork"],
"Bullseye / RTRT": ["Bullseye"],
"VectorCAST": ["VectorCAST"],
"Temppo": ["Temppo"],
"Valgrind": ["Valgrind"],
"Ubuntu": ["LX Client"],
"Confluence": ["Confluence"],
"Environment": ["Tools Front Desk"],
"Other Tools": ["Tools Front Desk"],
"Continuous Integration / Delivery": ["Continuous Integration"],
"Data External Exchanges": ["CS External Exchange", "JIRA Data Exchange"],
"Reporting Tools": ["PR Ramp Down"],
"Other Developed Tools": ["_DevOps"]
]
def issue = issue as MutableIssue
def selectList = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Tool/Area")
def selectListValue = issue.getCustomFieldValue(selectList) as LazyLoadedOption
// there is no value for the select list - therefore do nothing
if (!selectListValue) {
return
}
def componentManager = ComponentAccessor.projectComponentManager
def components = componentsMap.get(selectListValue.value)?.collect {
componentManager.findByComponentName(issue.projectObject.id, it)
}
if (components) {
componentManager.updateIssueProjectComponents(issue, components)
}
To briefly explain what it does: We have a custom field, select list type, called 'Tool/Area'. This is basically a way to group several components sometimes. People should select Tool/Area, instead of Components, and when they do that , some component gets added automatically to the issue.
Unfortunately, if you also choose another component, it gets replaced.
How could we change this to append the value to existing components, instead of replacing it?
Thank you!
I think I've done something like this before, but I don't have the code handy. I can at least try to provide you with the logic I think I used!
This is something similar.
Change the custom field to component
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
Issue issue = issue
def complianceField = customFieldManager.getCustomFieldObjectByName('Compliance Checks')
def complianceValue = issue.getCustomFieldValue(complianceField)
ArrayList <Option> selectedValues = (ArrayList<Option>) complianceValue
ArrayList <Option> newValues = new ArrayList<Option>();
if(complianceValue != null){
for(Option option : selectedValues){
if(option.getValue() != "Emergency Change"){
newValues.add(option)
}
}
issue.setCustomFieldValue(complianceField,newValues)
}
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 proposed solutions.
The script that works for me now is:
def issue = issue as MutableIssue
def selectList = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Tool/Area")
def selectListValue = issue.getCustomFieldValue(selectList) as LazyLoadedOption
// there is no value for the select list - therefore do nothing
if (!selectListValue) {
return
}
def componentManager = ComponentAccessor.projectComponentManager
def components = componentsMap.get(selectListValue.value)?.collect {
componentManager.findByComponentName(issue.projectObject.id, it)
}
def existing_components = issue.getComponentObjects();
def newValue = existing_components + components
if (newValue) {
componentManager.updateIssueProjectComponents(issue, newValue)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.