Forums

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

Scriptrunner syntax errors in Issue Organizations member emails script

Roshan Fernando October 2, 2023

Hi, Im getting syntax errors from the below script when I try to read email addresses from the Issue Organizations. It's returning the correct output in the log and the logic seems to be working. But for some reason it has syntax errors. Can someone please help? Thanks.

 

import com.adaptavist.hapi.jira.users.Users
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.SimplePagedRequest
import com.atlassian.servicedesk.internal.feature.organization.model.CustomerOrganizationImpl
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 issuesForOrganizations = Issues.getByKey('XXX-1010')
 
def customerOrg = issuesForOrganizations.getCustomFieldValue('Organizations') as List<CustomerOrganizationImpl>
 
def loggedInUser = Users.loggedInUser
def serviceDeskProject = serviceDeskManager.getServiceDeskForProject(issuesForOrganizations.projectObject)
 
def serviceDeskId = serviceDeskProject.id as Integer

// get the available organizations for that project
Boolean NextPageCheck = true
Integer StartRequest = 0
def organizationsResults = []

do {
    OrganizationsQuery organizationsQuery = organizationService.newOrganizationsQueryBuilder().pagedRequest(new SimplePagedRequest(StartRequest,50)).build()
    def result = organizationService.getOrganizations(loggedInUser, organizationsQuery)
    organizationsResults.addAll(result?.toList())
    NextPageCheck = result.hasNextPage()
    StartRequest = StartRequest + result.size()
    } while (NextPageCheck)

def usersInOrganizations = new StringBuilder()

 if (organizationsResults != null) {organizationsResults.each { organization ->
 customerOrg['name'].each {
 if (it == organization.name ) {
 def usersInOrganizationQuery = organizationService
.newUsersInOrganizationQuery()
.customerOrganization(organization)
.pagedRequest(new SimplePagedRequest(0, 50))
.build()
usersInOrganizations.append(organizationService.getUsersInOrganization(loggedInUser, usersInOrganizationQuery).results.collect {it.emailAddress}.join(',')).append(',')
}
}
}}
log.warn "====>>> ${usersInOrganizations.toString().replaceFirst('.$','') }"

Script Error.png

2 answers

1 accepted

0 votes
Answer accepted
PD Sheehan
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.
October 3, 2023

It appears that the code editor/code checker is losing track of what "organizationResults" is or should be. So it can't verify that "name" is valid. Then all the rest follows.

If static type-checking errors bother you (they are a tool for the developer, not a guarantee that your code is bad), you can force the matter by declaring your organization explicitly.

Something like:

List<CustomerOrganization> organizationsResults = []

Also, you might want to adjust your variable names. Initial capitals are reserved for class names. That's why the editor highlights  NextPageCheck and StartRequest in green.
By convention, those should start with a lowercase.

0 votes
Roshan Fernando October 4, 2023

Thanks @PD Sheehan

Your solution worked great!

Suggest an answer

Log in or Sign up to answer