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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

1 accepted

2 votes
Answer accepted

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

Like Manuela Sandrini likes this

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?

@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

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

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

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
}  

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

🍻🍂Apptoberfest Update: Upcoming Virtual Events 🎉

Hello Community! I hope you've been enjoying the 🍂Apptoberfestivities🍂 (I know I have!) The event is heating up next week with a series of virtual events that we're calling the 🍻🍂Partner App ...

684 views 7 23
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you