Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,365,180
Community Members
 
Community Events
168
Community Groups

Change User Picker field value in Service Desk

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

0 votes
Answer accepted

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

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.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events