ScriptRunner listener script to return member email addresses of SD Organizations as a string

Roshan Fernando September 10, 2023

Hi,

I'm trying to find a way to get a string of email addresses from Organizations (multiple) assigned to an issue. Ideally, I want to run this script through a custom listener and produce a string of email addresses separated by a comma. 

Can anyone help me with building the script using the latest HAPI interface?

 

Regards

Roshan

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 11, 2023

Hi @Roshan Fernando

Below is an example working code for your requirement:-

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.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 issue = event.issue
def loggedInUser = Users.loggedInUser
def customerOrg = issue.getCustomFieldValue('Organizations') as List<CustomerOrganizationImpl>

def project = issue.projectObject
def serviceDeskProject = serviceDeskManager.getServiceDeskForProject(project)

def serviceDeskId = serviceDeskProject.id as Integer
def organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId(serviceDeskId).build()
def result = organizationService.getOrganizations(loggedInUser, organizationsQuery)

def usersInOrganizations = new StringBuilder()

result.results.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.displayName}.join(',')).append(',')
}
}
}

log.warn "====>>> ${usersInOrganizations.toString().replaceFirst('.$','') }"

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Listener configuration:-

listener_config.png

I hope this helps to answer your question. :-)


Thank you and Kind regards,
Ram

Roshan Fernando September 11, 2023

Many thanks @Ram Kumar Aravindakshan _Adaptavist_ 

This is exactly what I was after. Thank you again!!!

Roshan Fernando October 2, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 
Can you please adjust the script to capture all available Organizations instead of the first 50? I can't figure out how to include customers from other pages on the Page Request.

Thanks in advance

Roshan

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events