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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,459,898
Community Members
 
Community Events
176
Community Groups

Copy Organisation into another project

Service Desk v3.3.0

jira v 7.3.0

 

Hi there

Wondering if anyone is aware of a s 'simple' method to copy all organisations defined in one service desk project to another service desk project?  

thanks
Maria

2 answers

Hi Maria, 

I would suggest to try the following script. I 'll be honest with you, I have not tested it thoroughly but give it a chance and if it fails shout will troubleshoot it together... 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.servicedesk.api.organization.CustomerOrganization
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.ServiceDeskService
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.util.paging.SimplePagedRequest
import com.atlassian.servicedesk.api.ServiceDesk
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

def projectFromKey = "ABCC"
def projectToKey = "ABCCD"
def asUser = ComponentAccessor.userManager.getUserByKey("admin")

def fromProject = ComponentAccessor.projectManager.getProjectObjByKey(projectFromKey)
def toProject = ComponentAccessor.projectManager.getProjectObjByKey(projectToKey)

getOrganizationsForProject(asUser, fromProject).each {
addOrganizationToProject(it, toProject)
}

void addOrganizationToProject (CustomerOrganization organization, Project project) {
def organisationService = ComponentAccessor.getOSGiComponentInstanceOfType(OrganizationService)
def adminUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
def sdProjectId = getServiceDeskForProject(project)?.id

if (! organization || ! sdProjectId) {
log.debug "Could not find organization $organization or service desk project $project"
return
}

def organizationServiceDeskUpdateParameters = organisationService
.newOrganizationServiceDeskUpdateParametersBuilder()
.organization(organization)
.serviceDeskId(sdProjectId)
.build()

def result = organisationService.addOrganizationToServiceDesk(adminUser, organizationServiceDeskUpdateParameters)
if (result.isLeft()) {
log.error "Could not add organization to project ${project.key}. Reason ${result.left().get()}"
}
}

ServiceDesk getServiceDeskForProject(Project sdProject) {
def serviceDeskManager = ComponentAccessor.getOSGiComponentInstanceOfType(ServiceDeskManager)
def serviceDeskProject = serviceDeskManager.getServiceDeskForProject(sdProject)

if (serviceDeskProject.isLeft()) {
log.error "${serviceDeskProject.left().get()}"
return null
}

serviceDeskProject.right().get()
}

List <CustomerOrganization> getOrganizationsForProject (ApplicationUser user, Project project) {
def serviceDeskService = ComponentAccessor.getOSGiComponentInstanceOfType(ServiceDeskService)
def srcServiceDesk = serviceDeskService.getServiceDeskForProject(user, project)

if (srcServiceDesk.isLeft()) {
log.error "Could not find service desk id for project ${project?.key}.\n${srcServiceDesk.left().get()}"
return []
}

def srcServiceDeskProjectId = srcServiceDesk.right().get().id
def organisationService = ComponentAccessor.getOSGiComponentInstanceOfType(OrganizationService)
def pagedRequest = new SimplePagedRequest(0, 50)
def organizationQuery = organisationService
.newOrganizationsQueryBuilder()
.pagedRequest(pagedRequest)
.serviceDeskId(srcServiceDeskProjectId)
.build()

// because this is a paged response it will return only the first 50 organizations in a project. If you want more you have to
// iterate over all pages and collect the results to a list
def pagedResponse = organisationService.getOrganizations(user, organizationQuery)
if (pagedResponse.isLeft()) {
log.error "${pagedResponse?.left()?.get()}"
}

pagedResponse.right().get().results
}

Paste this in the script console and ignore any errors you may get. Check the logs for "real" errors 

Let me know the result ...

Kind regards, Thanos

Thanks a lot, Thanos!

I tried to generate organizations with customers based on an insight object structure and struggled for a while.

Your script helped me, to understand the logic.

I'm in the process of finishing my script.

Best regards, Julia

0 votes

Hi Maria,

I have not seen this implelmented but I did find a post where ScriptRunner was being used.  It may require some additional tweaking on your end to get the final result to be what you want but hopefully this can help you get started.

Cheers,

Branden

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events