ScriptRunner Behaviour: Get string value of customfield dropdown

Deleted user October 5, 2018

Hello everyone

I've searched far and wide but couldn't find a solution appropriate for my issue.

In one of my listeners, I'm able to get the string value of my custom dropdown box like so.

def ComponentAccessor = com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def KasseCF = customFieldManager.getCustomFieldObjectByName("Kasse")

def issueKasse = issue.getCustomFieldValue(KasseCF)

This will return the exact displayed value and not the ID.

How can I get the same result in my behaviour?

I've tried using .getValue() and also almost the same setup above but instead used underlyingIssue?.getCustomFieldValue(KasseCF) since "issue" seems to be unavailable in behaviours but both options returned the ID of the selected option...

Can someone help me out here?

Thanks! 

3 answers

1 accepted

1 vote
Answer accepted
JohnsonHoward
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.
October 5, 2018

Hi Marius,

 

I am not sure if you are trying to get the value of a custom field which is on the dialog or a custom field that is on the issue underneath. Either way here I have provided different ways of dealing with options and getting custom field values etc.

try playing with some of this: 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

// different ways of getting field values
def yourField = getFieldById(getFieldChanged())
def yourFieldAnotherWay = getFieldByName("Your Field")
def yourFieldValue = yourField.getValue()

// get the options avaliable for that custom field
def customField = customFieldManager.getCustomFieldObject(yourField.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def getOptionById = ComponentAccessor.getOptionsManager().getOptions(config).find {it.optionId == yourFieldValue}

// get all the options and find the one for the id returned
def getOptionById = ComponentAccessor.getOptionsManager().getAllOptions().find{it.optionId == yourFieldValue}

Thannks

Johnson Howard (Adaptavist) 

Deleted user October 5, 2018

Hey Johnson

Thanks for your reply!

I am trying to get the string value of my customfield dropdown box which is on the create issue screen.

Depending on their selection, I would like to set the values of other fields.

All your suggestions only provide me with the ID of the selected option and not the text value of it.

Extract of my debug log:

[c.o.j.groovy.user.FieldBehaviours] Form field ID: customfield_11008, value: 10707

My script:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

import org.apache.log4j.Level
log.setLevel(Level.DEBUG)
/*log.debug("--------------------This is to test Behaviours Logging----------------")*/

def fieldChanged = getFieldById(getFieldChanged())

def getOptionById = ComponentAccessor.getOptionsManager().getAllOptions().find{it.optionId == fieldChanged.getValue()}

log.debug(fieldChanged)

if(fieldChanged == "customfield_11008")
{
log.debug(getOptionById)
}

 Regards,

Marius

JohnsonHoward
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.
October 5, 2018

Hi Marius, 

 

Have you tried doing this on the option that is returned: 

getOptionById.value
Deleted user October 5, 2018

This also returns the ID unfortunately :/

JohnsonHoward
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.
October 5, 2018
fieldChanged.value.toString()

try that 

Like Céline TAMBURRO likes this
Deleted user October 8, 2018

That was it! :)

Thanks a lot!

0 votes
Nishant Paraskar December 15, 2020

Here Select_List_custom is dropDown field

 

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customField = customFieldManager.getCustomFieldObject(getFieldChanged())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

 

Select_List_custom.setFieldOptions(options.findAll {it.value as String in ['India','USA','abc','pqr','xyx',.........]})

 

Try this pass value of whatever you have in dropdown in options.findAll  { } 

0 votes
Jyoti Verma July 25, 2019

Hi John

I have same requirement where based on selected value in Select list I need to make few fields hidden and others visible and also required.

 

def cfField = getFieldByName("Software Name")
def CatVal = getFieldById(getFieldChanged())

System.out.println("Cat Match::: " + CatVal)
def selectedOption = CatVal.getValue() as String
def isOtherSelected = selectedOption == "JIRA Account"
cfField.setHidden(true)

System.out.println("Cat Val::: " + selectedOption)
if(!isOtherSelected){
cfField.setHidden(false)
cfField.setRequired(true)
}

 

Issue if I am not able to get value at all - as my server logs only print first statement System.out.println("Cat Match::: " + CatVal) and not moving further. I used all possible options to get value but no luck. Can you please help here. I also used one mentioned above in discussion.

 

here is the server log:  you can see it just prints changed values of field but not moving further in script:

 

Cat Match::: Form field ID: customfield_10401, value: 10721
Cat Match::: Form field ID: customfield_10401, value: 10731
Cat Match::: Form field ID: customfield_10401, value: 10997
Cat Match::: Form field ID: customfield_10401, value: 10711

Deleted user July 25, 2019

Hey Jyoti

I suggest you post this as a new question. Johnson won't see this as he's not getting notifications from this, only me, the original poster.

Hijacking old and solved posts is bad practise anyway.

Best Regards

Marius

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events