Setting the value of a custom field through behaviors plugin

krmahadevan January 7, 2016

I have two custom fields both of them are Select List (Single choice). When creating these two custom fields, I am only providing "None" as the value.

From within the initialiser of a behavior, I am defining values for the first field as below.

Field A is called "Operating System" and has its values as "Windows, Linux, MAC".

The values for the second field, viz., Field B, is set via a getFieldById(fieldChanged) (This behavior is associated with the Field A so that it gets triggered everytime Field A's options are changed from the Select List)

Field B is called "Flavor" whose values are set based on the value chosen for Operating System.

So when "Windows" is chosen, then "Flavor" gets set to "98", "2000","XP"

When "MAC is chosen, then "Flavor" gets set to "Mountain Lion", "Yosenmite", "El Captain"

as its values.

When attempting to submit an issue with the above mentioned two fields, I get an error saying I have chosen an invalid value.

Was wondering if someone could please help me in this regard.

FWIW I am using JIRA v6.3.5

2 answers

1 accepted

2 votes
Answer accepted
Thanos Batagiannis [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 7, 2016

Hi Krishnan

This can be easily done with script runner and behaviours but I think for your case a select list cascading is what you need. 

EDIT: In case you want to use behaviours please have a look (and up-vote) a @Kristian Walker (Adaptavist) answer in a similar question Show/Hide options from select list

Kind regards

krmahadevan January 7, 2016

@Thanos Batagiannis [Adaptavist]

I am infact using behaviors with script runner in this case. The reason why I chose behaviors was because I have a few other fields which are to be filled up with values from an external system (by making HTTP calls and then parsing the response).

In either case, is there any way you could please help point me to some documentation (or) tutorial that I can refer to, to get this done ?

Even if I solved this use case with select list cascading (as you suggested) I still face the same problem with another control Select List (Multiple choices) which is also being filled up via a behavior wherein I make a HTTP GET call to an external service inside our company network to get its values. So I would really love to get some help through behaviors itself. Please advise.

Thanos Batagiannis [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 10, 2016

Hi Krishan I edited my answer above with a link to a similar question-answer, please let me know if you have any questions.

krmahadevan January 10, 2016

@Thanos Batagiannis [Adaptavist] Thanks for sharing that link. I was able to solve one part of my problem (Have one select list change its values based on the value chosen in another select list) by using what you recommended viz., Cascading select lists. But here's where i am stuck. I am constructing a select list (multiple values) dynamically by populating it with values from an external system. But when I am trying to submit an issue which contains this sort of a select list, I keep hitting an error from Jira. So how do I ensure that I can populate Select List dynamically at run time and still have Jira let me going ahead and submit an issue ? I am cross linking the stackoverflow query that I posted as well for additional information : http://stackoverflow.com/questions/34715970/dealing-with-dynamic-options-for-selectlist-in-jira-issue-creation

Thanos Batagiannis [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 10, 2016

What kind of error is this ? Could you please post it ?

krmahadevan January 10, 2016

Here's what Jira tells me "Invalid value '102' passed for customfield 'CASE-Environment'" Its as if Jira doesn't recognize any of the values that I added via the Behavior plugin's initializer. The initialiser looks like below import com.onresolve.jira.groovy.user.FieldBehaviours import com.onresolve.jira.groovy.user.FormField Map fieldOptions = [:] fieldOptions.put("-1","None") fieldOptions.put("100","Dev"); fieldOptions.put("101","Staging"); fieldOptions.put("102","QA"); fieldOptions.put("103","Production"); FormField env = getFieldByName("CASE-Environment"); env.setFieldOptions(fieldOptions) Here "CASE-Environment" is a Select List(Multiple values) I even tried adding a field mapping as below but the error continues to show up import com.atlassian.jira.issue.fields.CustomField import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.MutableIssue; import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.customfields.option.Options import com.atlassian.jira.issue.customfields.option.Option import com.onresolve.jira.groovy.user.FieldBehaviours import com.onresolve.jira.groovy.user.FormField def adddOption = { Issue issue, CustomField customField, String value -> Options l = ComponentAccessor.getOptionsManager().getOptions(customField.getRelevantConfig(issue)) int nextSequence = l.isEmpty() ? 1 : l.getRootOptions().size() + 1; Option newOption = ComponentAccessor.getOptionsManager().createOption(customField.getRelevantConfig(issue), null, (Long)nextSequence, value); return newOption; } FormField environment = getFieldById(fieldChanged) Issue issue = getUnderlyingIssue() MutableIssue mutableIssue = ComponentAccessor.getIssueManager().getIssueObject(issue.id); CustomField field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("CASE-Environment") List<Option> allOptions = new LinkedList<>(); List<String> chosenValues = (List<String>) mutableIssue.getCustomFieldValue(field); for (String eachValue : chosenValues) { allOptions.add(adddOption(issue, field, eachValue)); } mutableIssue.setCustomFieldValue(field, allOptions);

Thanos Batagiannis [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 11, 2016

Hi Krishnan, I think the problem is that every time you create the same options as new. For example the first time you will get the 5 options (None, Dev, Staging, QA, Production), the second time 10 options (the same duplicated) the third time 15... Also you use the underlyingIssue which means that this behaviour will work only if you edit and not during create (cause at that point the issue is not created yet, therefore is null).

0 votes
krmahadevan January 11, 2016

@Thanos Batagiannis [Adaptavist]

I cracked this on my own. I updated my query on Stackoverflow with the solution.

 

Here's a link to it as well. http://stackoverflow.com/a/34729156/679824

Suggest an answer

Log in or Sign up to answer