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

SWAPNIL SRIVASTAV November 1, 2019

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

0 votes
SWAPNIL SRIVASTAV November 1, 2019

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