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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Change User Picker field value in Service Desk

Adrián Plaza [DEISER]
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.
Nov 29, 2018

Hi Community,

 

The idea is, have on Jira only one field "Approver" created, and this field by default allows you to select one of the all users that you have in Jira.
Until here all is fine but now I want depends of the request type that the user select in the customer portal, show only the users of the one group.

I am traying doing something like this:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

FormField segAprueba = getFieldByName("Approver")
segAprueba.setLabel("Why do you need this?")
segAprueba.setDescription("Tell us why you want this.")

def users = ComponentAccessor.groupManager.getUsersInGroup("AAutomatizaciones JIRA")

ArrayList finalUsers = new ArrayList()

for (user in users){
finalUsers.add(user.key)
}

segAprueba.setFieldOptions(finalUsers)

 

But this doesn't work, have any ideas?

 

Adrián.

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Aidan Derossett [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.
Dec 17, 2018

Hey there Adrián!

Hopefully I'm not too late on this post! I've been toying around with this for the better part of a day and, using the approach described above, haven't been able to get this working either. I had suggested that you could set the field options of the user-picker, but I think I may have been off in that assumption. "Picker" fields are special in that they are dynamic; meaning the selectable options will change depending on what you type in. So, this makes it practically impossible to easily set their selectable options in any way.

However, I don't want to leave you without any option whatsoever. Through my messing around, I've come up with a method using ScriptRunner that could possibly work for your use-case. In short, I was able to find a way to dynamically populate the selectable options of a field depending on the request type. I accomplished this using two things, Behaviour select list conversions and Script REST Endpoints. You can get more information on these features through the documentation, but essentially the idea is that we convert a text field to a select field and populate it using the results of a REST Endpoint (which accepts a request type name as a query parameter) that returns a list of users. Below are some steps and code to accomplish this.

Steps:

  • Create a text custom field named "Approver"
  • Add that field to the to appropriate screens and request types in your project
  • Create a new Script REST Endpoint with the following in-line script. This endpoint is meant to take in a request type name (which you will need to customize) and return a JSON object of users from a group:
    import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
    import groovy.json.JsonBuilder
    import groovy.transform.BaseScript
    import javax.ws.rs.core.MultivaluedMap
    import javax.ws.rs.core.Response
    import com.atlassian.jira.component.ComponentAccessor

    @BaseScript CustomEndpointDelegate delegate

    setUserOptions(httpMethod: "GET") { MultivaluedMap queryParams ->
    def requestType = queryParams.getFirst("requestType") as String
    def rt = [:] //initialize return value

    def users = null
    switch(requestType) //update user values per request type
    {
    case "IT help":
    users = ComponentAccessor.groupManager.getUsersInGroup("IT help approvers")
    break
    case "<other request type>":
    users = ComponentAccessor.groupManager.getUsersInGroup("<other group name>")
    break
    //default:
    //etc...
    }

    rt = [
    items: users.collect{user ->
    [
    value: user.name,
    label: user.name,
    ]
    }
    ]

    return Response.ok(new JsonBuilder(rt).toString()).build();
    }
  • Next, navigate to your behaviour and add the following code to your initializer script. This code is meant to get the current request type and, using a select list conversion, send that request type to the above endpoint and create the select list:
    import com.onresolve.jira.groovy.user.FieldBehaviours
    import com.onresolve.scriptrunner.canned.jira.utils.servicedesk.ServiceDeskUtils
    import groovy.transform.BaseScript

    @BaseScript FieldBehaviours fieldBehaviours

    def requestTypeId = b.requestTypeId
    def serviceDeskId = b.serviceDeskId

    def requestType = ServiceDeskUtils.getRequestType(serviceDeskId as String,requestTypeId as String)

    log.debug("Request Type: ${requestType.name}")


    def segAprueba = getFieldByName("Approver")

    segAprueba.convertToSingleSelect(
    [
    ajaxOptions: [
    url : getBaseUrl() + "/rest/scriptrunner/latest/custom/setUserOptions",
    //query: false,
    data: [
    requestType : requestType.name,
    ],
    formatResponse: "general"
    ]
    ])
  • With both of these scripts in place and edited to your specifications, you should have a select field that is populated with a different set of users for every request type.

Try that out and let me know if it works for your purposes. :D

Best,
Aidan

Adrián Plaza [DEISER]
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.
Dec 20, 2018

Hi @Aidan Derossett [Adaptavist],

I don't have time to prove it until Christmas finish but this looks like I want.

Thanks a lot, Merry Christmas and a happy new year.

TAGS
AUG Leaders

Atlassian Community Events