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

Matt Leary
Contributor
August 14, 2017

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

1 accepted

2 votes
Answer accepted
Thanos Batagiannis [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.
August 14, 2017

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

Matt Leary
Contributor
August 15, 2017

Hi Thanos,

That works perfectly! Thanks! 

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

Cheers,

Matt

Thanos Batagiannis [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.
August 15, 2017

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

Thanos Batagiannis [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.
August 15, 2017

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 .... 

Like Manuela Sandrini likes this
Enkhtaivan Ganbat
Contributor
January 9, 2018

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

Thanos Batagiannis [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.
January 11, 2018

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

Enkhtaivan Ganbat
Contributor
May 29, 2018

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?

Tom R.
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 30, 2019

@Thanos Batagiannis [Adaptavist]  Hi, may I please know if it is possible to return more than 50 organizations with the query. I tried this: 

def organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId(serviceDeskId).pagedRequest(new SimplePagedRequest(0, 100)).build()

but it did not work. Thanks

Tom Hudgins
Contributor
October 2, 2020

I ran into the same problem of how to get more than the 100 users back from the organization. Then it dawned on me that I need to set the start value to the size of the last batch I got. 

So, you have to check the result of the query to see if it has a NextPage i.e. myResult.hasNextPage().

If it's true, you have to get myResult.size() and set as the start value in the pageRequest i.e. new SimplePagedRequest(startValue,100) and make the query again. You have to keep track of the next required startValue as you get new pages of results.

Put the whole thing in a loop until hasNextPage() is false and you should get all your users.

HTH

Maryum Mushtaq
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 6, 2021

Hi @Tom Hudgins,

I am trying the same logic but sadly it's not working fine for me. can you please share your script?

Many thanks

Tom Hudgins
Contributor
January 6, 2021

Here's a function that I use to get the orgs and users. Use the imports from the first post in this thread and that should get you the libraries you need. I've got this in a static class that does a bunch of other things so I'm not sure which of my imports are needed for this method.

HTH,

Tom

(Sorry, it has lost the indentation - I don't know how to get it to keep the indentation when I paste the code in a code block)

 

Map orgsAndUsersForProject(String projectKey) {

@WithPlugin("com.atlassian.servicedesk")

@PluginModule
ServiceDeskManager serviceDeskManager

@PluginModule
OrganizationService organizationService

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

// get the available organizations for the particular service desk
def serviceDeskId = serviceDeskProject?.id as Integer
def organizationsQuery = new OrganizationsQuery() {
@Override
Optional serviceDeskId() {
Optional opt = Optional.of(serviceDeskId);
return opt
}
@Override
LimitedPagedRequest pagedRequest() {
return new LimitedPagedRequestImpl(0, 100, 200)
}
}

def organizations = organizationService.getOrganizations(currentUser, organizationsQuery)
Map orgsAndUsers = [:]
organizations.each { org ->
def userList = []
def getNextPage = true
def startValue = 0
while (getNextPage) {
def usersInOrganizationQuery = organizationService
.newUsersInOrganizationQuery()
.customerOrganization(org)
.pagedRequest(new SimplePagedRequest(startValue, 100))
.build()
def usersInOrg = organizationService.getUsersInOrganization(currentUser, usersInOrganizationQuery)

usersInOrg.each { orgUser ->
userList.add(["Email": orgUser.getEmailAddress(), "Display Name": orgUser.getDisplayName()])
//orgsAndUsers[org.name] = orgUser.getEmailAddress()
}
if (usersInOrg.hasNextPage()) {
startValue = userList.size()
} else {
getNextPage = false
}
}
orgsAndUsers[org.name] = userList
}
return orgsAndUsers
}  
Like David Harkins likes this
David Harkins
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.
July 28, 2023

How would we go about getting all Organizations within the Jira Instance, regardless of which projects they are listed in?

David Harkins
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.
August 18, 2023

Like This :-)

 

Boolean NextPageCheck = true
Integer StartRequest = 0
def ServiceOrganizations = []

do {
    OrganizationsQuery organizationsQuery = organizationService.newOrganizationsQueryBuilder().pagedRequest(new SimplePagedRequest(StartRequest,50)).build()
    def result = organizationService.getOrganizations(UserASIWorkerBee, organizationsQuery)
    ServiceOrganizations.addAll(result?.toList())
    NextPageCheck = result.hasNextPage
    StartRequest = StartRequest + result.size()
} while (NextPageCheck)
Like Roshan Fernando likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events