Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get customers without organization thanks to scriptrunner ?

Deleted user September 29, 2020

Hello Community,

We ran the following script to get all new customers without organization, but it doesn't work anymore. Can someone help me please ?

Thanks,

 

import java.util.ArrayList

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

import com.atlassian.servicedesk.api.ServiceDesk
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.organization.CustomerOrganization
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.util.paging.PagedResponse
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('TEST')
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
ServiceDesk serviceDesk = serviceDeskProject?.right()?.get()
def serviceDeskId = serviceDesk.id as Integer

//def organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId(serviceDeskId).build()

import com.atlassian.fugue.Option
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

// 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, 100000, 100000)
}
}

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

ArrayList<String> userList = new ArrayList<String>()
ArrayList<String> organizationList = new ArrayList<String>()

PagedResponse<CustomerOrganization> organizations = (PagedResponse<CustomerOrganization>)result.right().get()
organizations.results?.each { organization ->
organizationList.add(organization.name)

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 {
ArrayList<String> userInOrgList = new ArrayList<String>()
PagedResponse<ApplicationUser> users = (PagedResponse<ApplicationUser>)usersInOrgResult.right().get()
for (ApplicationUser it : users.results) {
userList.add(it.emailAddress.toString())
userInOrgList.add(it.emailAddress.toString())
}
}
}

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.util.UserUtil

UserManager userManager = ComponentAccessor.getUserManager()
UserUtil userUtil = ComponentAccessor.getUserUtil()

def newCustomersGroup = ComponentAccessor.groupManager.getGroup("new-customers")
def supportCustomersGroup = ComponentAccessor.groupManager.getGroup("support-customers")

def newCustomersUsernames = ComponentAccessor.groupManager.getUserNamesInGroup(newCustomersGroup)
def supportCustomersUsernames = ComponentAccessor.groupManager.getUserNamesInGroup(supportCustomersGroup)

ArrayList<String> finalUserList = new ArrayList<String>()

userManager.getAllApplicationUsers().each{ u ->
if (userUtil.getGroupsForUser(u.getName()).size() == 0)
finalUserList.add(u.emailAddress.toString())

if ((newCustomersUsernames.contains(u.username)
|| supportCustomersUsernames.contains(u.username))
&& !userList.contains(u.emailAddress.toString()))
{
finalUserList.add(u.emailAddress.toString())
}
}

finalUserList.join("<br>")

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events