How can I get a list of the users for a given organization on a Service Desk using scriptrunner?

Hi,

I'm trying to get a list of the users currently in our service desk organizations using the scriptrunner plugin. Based on the scriptrunner documentation, I've been able to get the organizations out but can't get the customers back.

Is a method in the jira service desk OrganizationService (or other class) to get the organization users that I can call from the scriptrunner console?

What I've currently got:

import com.atlassian.fugue.Option
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.organization.OrganizationsQuery
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequest
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequestImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.jira.project.ProjectManager

@WithPlugin("com.atlassian.servicedesk")

@PluginModule
ServiceDeskManager serviceDeskManager

@PluginModule
OrganizationService organizationService

def projectObject = ComponentAccessor.getProjectManager().getProjectByCurrentKey('CCT')
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def serviceDeskProject = serviceDeskManager.getServiceDeskForProject(projectObject)

// if the project is not a Service Desk one then do nothing
if (serviceDeskProject.isLeft()) {
log.error "${serviceDeskProject?.left()?.get()}"
return
}

def serviceDeskId = serviceDeskProject?.right()?.get()?.id as Integer

// get the available organizations for that project
def organizationsQuery = new OrganizationsQuery() {
@Override
Option<Integer> serviceDeskId() {

return new Option.Some<Integer>(serviceDeskId)
}

@Override
LimitedPagedRequest pagedRequest() {
return new LimitedPagedRequestImpl(0, 50, 100)
}
}

// get all the organizations configured for that project
def organizations = organizationService.getOrganizations(currentUser, organizationsQuery)?.right()?.get()?.results

// get the Customers
def customers = []
// something here...

If there's documentation online for that I can refer to I'm happy to read through it. At the moment I'm using trial and error and the api docs with limited success ....

Thanks!

 

1 answer

Hey Matt,

If you want a script to run via the scirpt console, try this 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.util.paging.SimplePagedRequest
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

@PluginModule
ServiceDeskManager serviceDeskManager

@PluginModule
OrganizationService organizationService

def projectObject = ComponentAccessor.getProjectManager().getProjectByCurrentKey('ABC')
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def serviceDeskProject = serviceDeskManager.getServiceDeskForProject(projectObject)

// if the project is not a Service Desk one then do nothing
if (serviceDeskProject.isLeft()) {
log.error "${serviceDeskProject?.left()?.get()}"
return
}

// get the available organizations for the particular service desk
def serviceDeskId = serviceDeskProject?.right()?.get()?.id as Integer
def organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId(serviceDeskId).build()

def result = organizationService.getOrganizations(currentUser, organizationsQuery)
if (result.isLeft()) {
log.error "There was an error ${result.left().get()}"
}

def usersInOrganizations = ""
result.right().get().results?.each { organization ->
usersInOrganizations += "<h3> Organization : ${organization.name}</h3>"
def usersInOrganizationQuery = organizationService
.newUsersInOrganizationQuery()
.customerOrganization(organization)
.pagedRequest(new SimplePagedRequest(0, 50))
.build()

def usersInOrgResult = organizationService.getUsersInOrganization(currentUser, usersInOrganizationQuery)
if (usersInOrgResult.isLeft()) {
log.error "There was an error ${usersInOrgResult.left().get()}"
}
else {
def usersIn = usersInOrgResult.right().get().results.collect {it.displayName}.join("<br>")
usersInOrganizations += "$usersIn"
}
}

usersInOrganizations

Let me know if this did the trick ...

Kind regards, Thanos

Hi Thanos,

That works perfectly! Thanks! 

Is there a way to find the users not currently grouped into an organization?

Cheers,

Matt

Hey Matt, 

Glad that it works,  regarding your second question, I will need some time to find the right way to do this, Serivce Desk API is quite new and I need some time to explore it in depth .... 

Regards, Thanos

Also keep in mind that most of the calls with the Service Desk API return paged responses, which means that in the scirpt above it will return to you only the first 50 organizations if you want to ecpect more you will have tail all the paged responses to a collection .... 

Hello Thanos,

I've recently installed the ScriptRunner add-on, copied and ran your above script, I see errors in the script, yet I'm getting results. So I'm a bit confused, can you explain why something like this is possible?. I've attached the errors and result screenshots.Groovy script error.PNGUnexpected result.PNG

Hey Enkhtaivan,

It is quite possible for the errors you get in the in the UI to be caused because of the static type checking in a dynamic language (groovy)

In any case if there are any 'real errors' the application logs will tell you the truth. 

A way to avoid those error indications is to declare the types of your variables. 

Hope that answers your question. 

Kind regards, Thanos

Hello Thanos,

 Your script above won't work on JIRA Cloud right? Do you have a cloud version or can you recommend me a some guide/resource?

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Thursday in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

518 views 3 5
Join discussion

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