Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script runner: setFieldOptions for custom picker field

Yin Liang_XDevPod
Atlassian Partner
March 25, 2024

Hello

I have applied some  custom picker fields (e.g. user picker for a specified group) with script runner. 

Is it possible to use setFieldOptions of  behaviour  to restrict the selections to a smaller scope ?

2 answers

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 25, 2024 edited

Hi @Yin Liang_XDevPod

If you are using a Custom Picker field, setting the field options via Behaviour is not feasible as this is controlled by the configuration you have set for the Custom Picker.

Since you intend to create a custom field based on the User's Group, why not just use Behaviour to convert a Text Field to a List and pull the options from the REST Endpoint? You'll have more control and can filter the options accordingly from there.

I have given a similar solution in this Community Post some time back.

The sample provided in the Community Post is filtering the users by User Role. You will need to modify it to filter it by Group instead.

Thank you and Kind regards,
Ram

 

0 votes
Sean Chua _Adaptavist_
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.
March 25, 2024

Hey @Yin Liang_XDevPod ,

Yes, that would be possible. There are some existing samples here which you can use as reference:

Hope it helps.

Sean

Yin Liang_XDevPod
Atlassian Partner
March 25, 2024

@Sean Chua _Adaptavist_ 

 

Thank you very much for your quick answer.

 

The filed to "setFieldOptions" is not system fields or  custom select field,  it's the "Custom Picker" filed created via script runner.

Pls see below code for the picker 

 

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.fields.model.PickerOption

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.avatar.Avatar


def groupManager = ComponentAccessor.getGroupManager()
def userManager = ComponentAccessor.getUserManager()
def avatarService = ComponentAccessor.avatarService
def authenticationContext = ComponentAccessor.jiraAuthenticationContext

def group = groupManager.getGroup("team1")
def users = group.getMembers()


search = { String inputValue ->
return users
}

getItemFromId = {String id ->
userManager.getUserByKey(id)
}

toOption = { ApplicationUser user, Closure<String> highlight ->
def remoteUser = authenticationContext.getLoggedInUser()


new PickerOption(
value: user.key,
label: user.name,
html: highlight(user.displayName, false),
icon: avatarService.getAvatarURL(remoteUser, user, Avatar.Size.SMALL)?.toString()
)
}

 

how to set options  for such field ? what's the input parameter for setFieldOptions()?

Sean Chua _Adaptavist_
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.
March 25, 2024

Hey Yin Liang,

Thanks for the callout and I do apologise for reading the question wrongly - I must have been groggy from not having my morning coffee yet. It is as what you and ram has pointed out.

I would have gone with Ram's suggestion to use a Text field which is also feasible and he also shared his solution;

I have given a similar solution in this Community Post some time back.
The sample provided in the Community Post is filtering the users by User Role. You will need to modify it to filter it by Group instead.

Regards,
Sean

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.14
TAGS
atlassian, atlassian government cloud, fedramp, webinar, register for webinar, atlassian cloud webinar, fedramp moderate offering, work faster with cloud

Unlocking the future with Atlassian Government Cloud ☁️

Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.

Register Now
AUG Leaders

Atlassian Community Events