Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Set field options based on another field selection

Thomas Hardin
Contributor
June 19, 2018

I have been working to find a solution to support the following scenario and need some help.

We have a project that our management uses to track project funding sources. These funding sources are tied to specific projects and releases in Jira. Now, they want to have that project funding code automatically populated within the issues of the target projects. 

Now, I've looked at the main project and see some issues, so I am adding a project picker field ("Project Name") and a multi-select drop down ("Project Version") so that the management team can better track their data. 

1st, what I want to do is to have the project picker field display only the version of the selected project so that the user only sees the projects versions in the drop down list. To help, I pulled all projects and versions from the jira db and used the scriptrunner Built-In script to populate the 2 fields (Project Name and Project Version). I've also taken the All Previous Versions script from the documentation and modified it a little to get the versions of a project using an issue key for testing and I at least get the right results.

package com.onresolve.jira.groovy.test.scriptfields.scripts

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comparator.VersionComparator

def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("WSC-80")
def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersions(issue.projectObject)
def comparator = new VersionComparator()
def lowestFixVersion = issue.fixVersions.min(comparator)
def returnedVersions = versions.findAll{
comparator.compare(it, lowestFixVersion) < 0
}
log.debug("All prior versions: ${returnedVersions}")
return (lowestFixVersion ? returnedVersions : null)
return versions

 

Now, I'd like to somehow use the above script to take the project selected in the project picker field to generate the results and then use a behavior to only show those options in the project version field.

Any help is greatly appreciated.

2 answers

1 vote
Danyal Iqbal
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.
June 19, 2018

forget the above script.

Workaround:

* create project version field as a checkboxes type field and add all options.

* use a behaviour to hide project versions based on the project picker field.

Thomas Hardin
Contributor
June 19, 2018

Do you have an example? Maybe a sample snippet of code?

Danyal Iqbal
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.
June 20, 2018

You should be able to find a proper example here:

https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html#_examples

 a js snippet to hack away if you have had enough with jira and doing things the proper way:

<script type="text/javascript">
source=document.getElementById('customfield_15428')//project field
if (source) {
      if (source.value == 21544)//check value of project field
{
$('#customfield_14401-2').parent().hide() //14401 is the project version field & 14401-2 is the second option
$('#customfield_14401-3').parent().hide() //14401 is the project version field & 14401-3 is the second option
}
 source.onchange=function() {
          if (this.value == 21544) {
$('#customfield_14401-2').parent().hide() //14401 is the project version field & 14401-2 is the second option
$('#customfield_14401-3').parent().hide() //14401 is the project version field & 14401-3 is the second option

                  }

          if (this.value != 21544) {              
//so something on change here as well :)
                  }
            
      }
  }
an so on
}

put this in the description of any text field on the page.

Like Network Operations likes this
0 votes
Thomas Hardin
Contributor
June 26, 2018

Good morning,

I've made some strides with this effort, but am not quite there yet. I've taken some examples from the community and have been able to populate my versions field with the applicable values from a particular project, but that value was placed in the script. I am still missing a piece where I can use the actual value from the project picker. My thought is that I need to somehow define the field value, but I can't seem to figure it out. Here is the latest code:

def issueManager = ComponentAccessor.getIssueManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

//Define Project Name custom field
def projectName = getFieldByName("Project Name")
def projectNameField = customFieldManager.getCustomFieldObject(projectName.getFieldId())
def projectNameConfig = projectNameField.getRelevantConfig(getIssueContext())
def projectNameOptions = optionsManager.getOptions(projectNameConfig)
String projectNameValue = projectName.getValue()

//Define Project Version custom field

def projectManager = ComponentAccessor.getProjectManager()
def project = projectManager.getProjectObjByName("Websites (Core)")

def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersions(project)


def versionOptionsFld = getFieldByName("Project Version")

versionOptionsFld.setFieldOptions(versions)

 

As you can see, when I use a static value in the code, "Websites (Core)", I am able to populate the Versions drop down field. This works. I just need to somehow, get the value from the project picker field , "Project Name", to be the source of that data.

 

Thank you all for the continued support. Any help is always greatly appreciated.

Suggest an answer

Log in or Sign up to answer