Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

ScriptRunner Behaviour: Get string value of customfield dropdown

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! 

2 answers

1 accepted

0 votes
Answer accepted

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) 

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

Hi Marius, 

 

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

getOptionById.value

This also returns the ID unfortunately :/

fieldChanged.value.toString()

try that 

That was it! :)

Thanks a lot!

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

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

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