Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,361,708
Community Members
 
Community Events
168
Community Groups

Spriptrunner listener to set value in select list based on input entered in a text field

Edited

Hello,

 

Thanks in advance! I have an issue with the script i wrote, to implement business requirement, editor shows that al checks passed and to me script seems fine, however im not a developer.

 

Use case: 

1. issue is created via API (text box that will determine the mapping is populated 100% of the times)

2.  We need to set value in Single select list based on the input form the text field (example, if text says "Hello" set destination list value to "greeting"

 

Seemed pretty simple, but for some reason values are not set, even though logs show successful execution.

Any help will be greatly appreciated, we are trying to scale this solution. 

PS. values in the destination field exist in Jira and field is placed on all appropriate screens

Please find script below:


import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.MutableIssue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def dataclassCF = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Data Class"}

def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "Data Class test"}

def issue = event.issue as Issue

def issueService = ComponentAccessor.getIssueService()

def issueManager = ComponentAccessor.getIssueManager()

MutableIssue issueToUpdate = (MutableIssue) issue;

log.error(event.issue.getCustomFieldValue(dataclassCF).toString().contains("New Data Class"))

if (event.issue.getCustomFieldValue(dataclassCF).toString().toLowerCase().contains("allergies and intolerances")) {

   issueToUpdate.setCustomFieldValue(tgtField, "1")

} else if (event.issue.getCustomFieldValue(dataclassCF).toString().toLowerCase().contains("assessment and plan of treatment")) {

  issueToUpdate.setCustomFieldValue(tgtField, "2")

}

1 answer

1 accepted

Hello Alex,

I'm Olly from Adaptavist's ScriptRunner Support!

The reason this isn't working is because of the way Select Lists have to be set.

I would recommend checking out our Library Script: Update a Single-Select (Drop Down) Custom Field which should solve this issue!

Here's a brief explanation of what's going on in that script:

The method you're using would usually work for a typical field, such as Number or Text:

issue.setCustomFieldValue(exampleField, "<VALUE_TO_SET>")

However, for a Select List Field;

  • You need to find the Select List's Options
  • As well as the relevant Issue Config 
  • To make sure the newValue actually exists as an option for the Select List
  • And if it does not, the assert will display:
def availableOptions = ComponentAccessor.optionsManager.getOptions(customField.getRelevantConfig(issue))
def optionToSet = availableOptions.find { it.value == newValue }
assert optionToSet: "Could not find option with value $newValue. Available options are ${availableOptions*.value.join(",")}"

Afterwards, you can finally set the value with the customField.updateValue() method:

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())

Hope this answers your query!

Thanks,
Olly 

Thanks Olly, 

We will try this, do i need to define each option separately?

i have over 50 options to map, just want to make sure.

No problem Alex!

I think so, however you don't need to create a long list of 50 newValues, here's what I mean:

Using the Library Script, you have this:

final newValue = 'Option C'

Which essentially is the new option you're setting the Select List to.

However, in your Script you have conditional if statements.
This will allow you to define the Option separately for each if condition.

So, it could look something like:

...

def availableOptions = ComponentAccessor.optionsManager.getOptions(customField.getRelevantConfig(issue))

if (event.issue.getCustomFieldValue(dataclassCF).toString().toLowerCase().contains("allergies and intolerances")) {

def newValue = 'Option 1'

def optionToSet = availableOptions.find { it.value == newValue }
assert optionToSet: "Could not find option with value $newValue. Available options are ${availableOptions*.value.join(",")}"

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())

} else if (event.issue.getCustomFieldValue(dataclassCF).toString().toLowerCase().contains("assessment and plan of treatment")) {

def newValue = 'Option 2'

def optionToSet = availableOptions.find { it.value == newValue }
assert optionToSet: "Could not find option with value $newValue. Available options are ${availableOptions*.value.join(",")}"

customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
} else if ...

If you wanted to be super efficient with your code duplication, you could create a method which calls this customField.updateValue() method with all the necessary values.
However, it's not super necessary.

Thanks,
Olly

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.22.4
TAGS
Community showcase
Published in Jira Software

An update on Jira Software customer feedback – June 2022

Hello Atlassian Community! Feedback from customers like you has helped us shape and improve Jira Software. As Head of Product, Jira Software, I wanted to take this opportunity to share an update on...

5,078 views 18 32
Read article

Atlassian Community Events