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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Duplicate query results by using Organization groovy API

Hi,

I'm using Scriptrunner in our Jira Service Management Datacenter setup to manage Organizations.

When I run the script (see below) the Organization query returns duplicates i.e. the same Organization is returned on different pages in response' pagedresult. When I run the script multiple times consequently, then usually first couple of times I do get duplicates, but then Jira returns duplicate-less response.

What could be the root cause here?

Kind Regards,

Andrey

import javax.ws.rs.core.MultivaluedMap 
import static groovyx.net.http.ContentType.*

import org.apache.log4j.Level
import org.apache.log4j.Logger

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

@WithPlugin("com.atlassian.servicedesk")

// set up a logger
def lgr = Logger.getLogger("GettingOrgs")
lgr.setLevel(Level.DEBUG)

def isLastPageReached = false
def startIndex = 0
ArrayList jsdOrgsList = []
def infiniteLoopPrevention = 10000
def duplicatesCounter = 0

def adminUser = ComponentAccessor.userManager.getUserByKey('JIRAUSER10300')
def organisationService = ComponentAccessor.getOSGiComponentInstanceOfType(OrganizationService)

while (!isLastPageReached && (infiniteLoopPrevention-- >0)) {

def organizationQuery = organisationService
.newOrganizationsQueryBuilder()
.serviceDeskId(1)
.pagedRequest(SimplePagedRequest.paged(startIndex, 6000))
.build()

def pagedRes = organisationService.getOrganizations(adminUser, organizationQuery)

pagedRes.getResults().each { pagedResElement->

if (jsdOrgsList.find{it.jsdId == pagedResElement.id} == null) {
jsdOrgsList.add([
jsdId: pagedResElement.id
])

} else {
duplicatesCounter++
}
}

startIndex +=50
isLastPageReached = !pagedRes.hasNextPage()
}

lgr.debug("duplicatesCounter = ${duplicatesCounter}")
lgr.debug("Unique orgs count on all pages = ${jsdOrgsList.size()}")
def sumtotal = duplicatesCounter + jsdOrgsList.size()
lgr.debug("Sum of unique count and duplicates = ${sumtotal}")

1 answer

Hell Andrey,

I think I see the error:

.pagedRequest(SimplePagedRequest.paged(startIndex, 6000))

You ask the api to retrieve 6000 rows, starting from startIndex, but the limit for each paged request is 50, so the api only retrieves 50.

Try changing it it to:

.pagedRequest(SimplePagedRequest.paged(startIndex, 50))

 

Let me know if this helped!

Regards,

Jeroen 

Hello Jeroen,

thank you for your response. I have tested the query with different values like 50, 6000, 30... The result is always the same: duplicates sometimes occur.

So far, I've found an workaround: I've put the query into a loop with a threshold on iterations count. After a couple of iterations, jira returns the Orgs without duplicates. It means, the same code returns for the same data different results. This lets us assume, that we have some caching / nodes synchronization issue.

Regards,

Andrey

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

163 views 0 4
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