Hi,
I have two custom fields, say X and Y in the issuetype -bug.
I want the following to happen, after the bug is created -
1. Say X has the value "ABCD-1234". I want to truncate the value of X by removing the text after -, so that I get "ABCD" only
2. Now I want to update/replace the value of the another custom field i.e. Y as "ABCD" ( the value extracted above)
The field Y is a Select List (single choice)
Please help me how do I achieve this?
Any help is appreciated
Thanks in advance
Bhawna
Hi @Bhawna Sharma,
Below script may give you some idea, I haven't tried in my instance. it may need some minor changes
Note: I've written this one for Script Listener, if you are going to run through post-function it needs minor change in field update query
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
def myIssue = event.issue
def optionsManager = ComponentAccessor.getOptionsManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfSource = myIssue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Source/Text Field")) as String
if(cfSource){
def sourceValue = cfSource.split('-') as List
def convert = "ABCD-"+sourceValue[1]
def cfSelect = customFieldManager.getCustomFieldObjectByName("Select List Field Name")
def fieldConfig = cfSelect.getRelevantConfig(myIssue)
def optionsICUR = optionsManager.getOptions(fieldConfig)
def optionToSet = optionsICUR.find{it.value == convert}
def changeHolder = new DefaultIssueChangeHolder();
cfSelect.updateValue(null,myIssue, new ModifiedValue(myIssue.getCustomFieldValue(cfSelect),optionToSet),changeHolder)
}
BR,
Leo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Leo
I tried and tested exactly the same script.
However, in below- optionToSet is set to NULL
def optionToSet = optionsICUR.find{it.value == convert}
I am unable to figure out the reason. The value of convert is correct
Also, if you can pls help me explain the above script statement
Thanks
Bhawna
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You said, the 2nd field Y is a select list which you need to update with new value right?
if so, that select list field should have this value as a option already e.g. ABCD-1234
This line will find the option from those list, if that value is not available as a option in select list field, will assign NULL to variable
I'm not sure what is your requirement, but to update values like this I would suggest single line text field rather than select list field, because the values may keep on changing and you can't add all of them as options to target field every time
If you are going to use Text field below script should work
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
def myIssue = event.issue
def optionsManager = ComponentAccessor.getOptionsManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfSource = myIssue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Source/Text Field")) as String
if(cfSource){
def sourceValue = cfSource.split('-') as List
def convert = "ABCD-"+sourceValue[1]
def cfTarget = customFieldManager.getCustomFieldObjectByName("Target Field Name")
def changeHolder = new DefaultIssueChangeHolder();
cfTarget.updateValue(null,myIssue, new ModifiedValue(myIssue.getCustomFieldValue(cfTarget),convert),changeHolder)
}
BR,
Leo
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Leo,
My answers inline
You said, the 2nd field Y is a select list which you need to update with new value right?
Bhawna - Yes, that's right
if so, that select list field should have this value as a option already e.g. ABCD-1234
Bhawna-No. The field Y will not have that as a option already. Say Y has the current value set as "Generic". I want to replace this existing value with "ABCD" . I want the Y field to show me the value as "ABCD" filled in
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Below is the script I used long ago for adding options to select list dynamically
you may need to tweak the script with combination of above one to get succeed
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def issueService = ComponentAccessor.getIssueService()
def issue = event.issue
def value = issue.summary as String
def updField = customFieldManager.getCustomFieldObjectByName("Field name for which you want to add new values/options")
def fieldConfig = updField.getRelevantConfig(issue)
def currentOptions = optionsManager.getOptions(fieldConfig)
def newSeqId = currentOptions*.sequence.max() - 1
def option = optionsManager.createOption(fieldConfig, null, newSeqId, value)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {
addCustomFieldValue(updField, option.optionId.toString())
addCustomFieldValue(value, null)
}
def updateValidationResult = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)
if (updateValidationResult.isValid()) {
issueService.update(currentUser, updateValidationResult)
}
else {
log.warn("Failed to update issue: ${issue.key}: ${updateValidationResult.errorCollection}")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot Leo
Worked on it but it did not work. It looks like that it is not possible to update the select list field
so I created new single line text field for my purpose.
Regards
Bhawna
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.