Match an option from a drop down menu based on an LDAP custom field Edited

Hi, 

I need to set an option (from a custom drop down menu in JIRA Service Desk or ~860+ options) what is set in this drop down menu is based on a 4 digit code that is pulled in from a LDAP connection for every user that submits a ticket.

The drop down menu is a custom concatentation of the 4 digit code and it's description (which doesn't exist in ADFS but is VERY useful). What I was thinking was I'd need to incorporate RegEx in a way that says okay match the 4 digits in LDAP field (customfield_10403) to the first 4 digits in custom drop down menu (customfield_10504)

I have scriptrunner installed but I've only come across threads that accomplish only one part of what I need to do and I'm very new to scripting. I am also open to going a different route, as long as the end goal is accomplished.

Thanks!

Whitni

1 answer

1 accepted

1 vote
Jenna Davis Community Champion Jun 29, 2017

Hello, 

Sorry for such a late reply!

I'd love to help you out with this, but I need you to clarify some of the details for me. 

First, how and where are you wanting to display this (the create screen as a behavior or the view screen as a scripted field etc.)? Be aware that behaviors only work on certain screens and that scripted fields don't show up on the create and update/edit screens. That might limit your choices a bit depending on what you want. :) I think you're wanting a behavior, but just to check.

Also, just to make sure I understand. You want to generate numbers that are a concatenation of a 4 digit code stored in one field that is unique to the user and other 4 digit codes that are stored in a list. The concatenated versions will then be added as options into a drop down menu.

If that is what you're wanting, it is deinfately possible. You could simply make a list that stored the 4 digit values and then have you LDAP custom field value. Then you could use a behavior to go through each value in the list, concatenate it with the LDAP field and add it as an option in the drop down.

 If you could clarify anything I got wrong, and maybe go into a bit more detail on anything specific you want that would be great.  

Also, if you could outline what you've tried already, or send me any code you have started that would help as well. 

As far as getting started on your own, the ScriptRunner documentation is a great resource. 

Hopefully I can help you out!

Jenna

Hi Jenna,

This particular field would be on the create screen as a behavior, I'm assuming. 

What we want to do is we have a 2 custom fields, one that is autopopulated via an LDAP connection pulling a field from ADFS, this is the 4 digit field (it already exists we are not creating it), and the second custom field is a drop down menu, where each menu item is a string that starts with the 4 digit code, followed by a description. What I want to do is match the custom field from ADFS to the drop down menu option. So rather than concatenate we want to match the 4 digit code to the first 4 digits of the values in the drop down menu and when it finds a match to set it as that option. 

Example:

Custom field 10403 == 1234

Custom field 10404 == 1234 - the description

field 10403 is pulled in from ADFS, field 10404 is a drop down menu that I have created manually. 

If CF 10403 == 1234

Search CF 10404 /\d{4}/  and if 1234 exists in CF 10404 then set that option. 

I haven't written any code for it because I've been digging through the documentation and scripting fields in JIRA is *very* new to me.  

I've come across issuefieldmatch 

https://scriptrunner.adaptavist.com/latest/jira/jql-functions.html#_issuefieldmatch

which seems like a viable option except I'm not looking for issues with particular information but I want to search custom fields and set them... 

Jenna Davis Community Champion Jun 30, 2017

I don't believe you'll want to use JQL functions for this. If you look at the behavior examples you may find something helpful there.

This should get you started, it's set up to only take the first value that matches the condition that it finds. If you want to be able to change the dropdown so that it displays multiple matching values I can help you with that as well.

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

// you'll want to change these to match your field name
// you can use getFieldById instead if you'd prefer
def dropdown = getFieldByName("dropdown")
def ldap = getFieldByName("ldap")

def customField = customFieldManager.getCustomFieldObject(dropdown.fieldId)
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

def ldapVal = ldap.getValue()

def selectedOption = options.find{ it.value.substring(0,4) == (ldapVal as String)}

dropdown.setFormValue(selectedOption.optionId )
dropdown.setReadOnly(true)

Also, I made the values readonly at the end. If you want the user to be able to change it and just have this value set as a default, remove the last line. :)

I was testing with this as an behavior initializer function, I believe you'll want it set up the same way from what I understand.

Hopefully this helps!

Let me know if you have any questions.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

72 views 0 5
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you