It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to add dynamic options into a Select List custom field using groovy

I want to fetch the value of a custom field(Number type) and system field "Summary" and concatenate the two to make options of a select list custom field. 

The issues are fetched by JQL query which is triggered in the same behavior and the results can be iterated.

Could anyone please help me with that.

Any help would be appreciated.

Thanks,

Swapnil Srivastav

 

1 answer

Here is the code that I tired:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.search.SearchProvider

import com.atlassian.jira.jql.parser.JqlQueryParser

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.groovy.user.FieldBehaviours

import com.atlassian.jira..issue.customfields.manager.OptionsManager

import com.atlassian.jira.issue.IssueManager

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

 

if(getFieldChanged())

{

  def customFieldManager = ComponentAccessor.getCustomFieldManager()

  def umbApp = customFieldManager.getCustomFieldObjectByName("Name of select list")

 def optionsManager = ComponentAccessor.getOptionsManager()

 def fieldConfig = umbApp.gettRelevantConfig(getIssueContext())

 def options = optionsManager.getOptions(fieldConfig )

 def umbAppMap =[:]

 umbAppMap.put("-1", "None")

 def uNum = customFieldManager.getCustomFieldObjectByName("Name of Number field")

 

 def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)

 def searchProvider = ComponentAccessor.getComponent(SearchProvider)

 def issueManager = ComponentAccessor.getIssueManager()

 def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 def query = jqlQueryParser.parseQuery("project ='XXX' AND issuetype = XXX")

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

 

if(results.total>0)

{

   results.getIssue().each{ documentIssue ->

      def issue = issueManager.getIssueObject(documentIssue.id)

      def unumValue = (Integer) issue.getCustomFieldValue(unum)

      def umbAppOption = "U-"+unumValue+" "+issue.summary

      umbAppMap += options.findAll { it.value in [umbAppOption]}.colectEntries{

         [(it.optionId.toString()) : it.value]}

}

}

umpApp.setFieldOptions(umbAppMap)

}

 

 

 

Even if I set the options as (just to test):

umbAppMap += options.findAll { it.value in["Hello", "Hi", "Hola", "Adios"]}.collectEntries{

[(it.optionId.toString()): it.value]}

 

Then also, I am getting an error in the last line:

umbApp.setFieldOptions(umbAppMap)

The error is :

Cannot find matching method.

 

Kindly correct the code

Regards,

Swapnil

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

342 views 1 3
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you