Need help to auto update Organization field in Jira SD when issue is created on customer portal

Dhavval Aarya June 4, 2019

Hello, I am new to jira and scripting scriptrunner/groovy:
I am currently running Jira 8.0.2 and SD 4.0.2 with scriptrunner 5.5.6.1-jira8. I got following error when I created the an incident as agent for external (new user) by using a script listner on create event. I copied the script from below.

link: https://scriptrunner.adaptavist.com/latest/jira/blog/GettingMoreOutOfJsd.html

script:

import com.atlassian.fugue.Option
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
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.LimitedPagedRequest
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequestImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

@PluginModule
ServiceDeskManager serviceDeskManager

@PluginModule
OrganizationService organizationService

MutableIssue issue = issue

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def serviceDeskProject = serviceDeskManager.getServiceDeskForProject(issue.projectObject)

// if the project is not a Service Desk one then do nothing
if (serviceDeskProject.isLeft()) {
log.error "${serviceDeskProject?.left()?.get()}"
return
}

def serviceDeskId = serviceDeskProject?.right()?.get()?.id as Integer

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

// get all the organizations configured for that project
def organizationsToAdd = organizationService.getOrganizations(currentUser, organizationsQuery)?.right()?.get()?.results

// get the Organizations custom field
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Organizations")

// finally update the organizations custom field
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), organizationsToAdd), new DefaultIssueChangeHolder())

2019-06-04 12:57:23,560 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-06-04 12:57:23,562 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script152.groovy: 37: The return type of com.atlassian.fugue.Option serviceDeskId() in Script152$1 is incompatible with java.util.Optional in com.atlassian.servicedesk.api.organization.OrganizationsQuery
. At [37:5]  @ line 37, column 5.
       @Override
       ^
1 error

 

actual requirement is to auto-fill Organization field based on the email domain of the requester(customer), whenever a new issue is created by requester or agent(on behalf of customer) from the customer portal . I am setting up a new project and thus there is no existing organization in the project.

Any help would be really appreciated.

Best  Regards,

Dhavval

1 answer

1 accepted

9 votes
Answer accepted
Sergey Zaporozhtsev [Adaptavist] August 1, 2019

Hi @Dhavval Aarya I'm Sergey and I work on the ScriptRunner team at Adaptavist.

Script from https://scriptrunner.adaptavist.com/latest/jira/blog/GettingMoreOutOfJsd.html is designed to work with JSD before 4.x. Since 4.0 some API are changed and script need to be modified.

You can use this version of the original script as your starting point.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
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.organization.OrganizationsQuery
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequest
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequestImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

@PluginModule
ServiceDeskManager serviceDeskManager

@PluginModule
OrganizationService organizationService

MutableIssue issue = issue

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ServiceDesk serviceDeskProject

try {
serviceDeskProject = serviceDeskManager.getServiceDeskForProject(issue.projectObject)
} catch (ignored) {
// if the project is not a Service Desk one then do nothing
return
}

def serviceDeskId = serviceDeskProject?.id as Integer

// get the available organizations for that project
def organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId(serviceDeskId).build()

// get all the organizations configured for that project
List<CustomerOrganization> organizationsToAdd

try {
organizationsToAdd = organizationService.getOrganizations(currentUser, organizationsQuery)?.results
} catch(ignored) {
// no organizations found or no access
return
}

// get the Organizations custom field
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Organizations").first()

// finally update the organizations custom field
cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), organizationsToAdd), new DefaultIssueChangeHolder())

 

Best regards,

Sergey

Dhavval Aarya August 2, 2019

Hello @Sergey Zaporozhtsev [Adaptavist] ,

Thank you so much for sharing updated script. I works fine when the organisation name similar to email domain already exists in Jira service desk project but does nothing when organisation name is not in the list.

I am new to scriptrunner and Jira and trying to set it up. Can you please be kind enough to help me with this requirement that, if organisation doesn't exist in the list, create one.

moreover I found that, updating organisation field does not mean updating membership of a organisation with the customer, which means, customer does not get added to org. I also had to manually add customer in the organisation.

Thank you in advance.

Best Regards,

Dhavval

Sergey Zaporozhtsev [Adaptavist] August 2, 2019

Hi @Dhavval Aarya 

You can reference https://docs.atlassian.com/jira-servicedesk/4.0.0/com/atlassian/servicedesk/api/organization/OrganizationService.html for Organization APIs

 

I.e. you can use the following code snippet to create organization and associate it with service desk:

def createParams = organizationService.newCreateBuilder().name("New Organization").build()
def org = organizationService.createOrganization(currentUser, createParams)

def updateParams = organizationService.newOrganizationServiceDeskUpdateParametersBuilder().serviceDeskId(serviceDeskId).organization(org).build()
organizationService.addOrganizationToServiceDesk(currentUser, updateParams)

 

And this snippet to add users to organization:

def usersToAdd = ...
def addUsersParam = organizationService.newUsersOrganizationUpdateParametersBuilder().organization(org).users(usersToAdd).build()
organizationService.addUsersToOrganization(addUsersParam)

 

Best regards,

Sergey

Like # people like this
George Lewe (LSY)
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.
May 4, 2021

Hi there,

I do not need to create an organization or add users to one.

I would like to add an existing organization to the Organizations field when an issue is created.

Let's say I have an organization "MYORG" and would like to add it to the issue's Organizations field when it is created.

How can I do that? I tried several combinations of the snippets above but they all throw errors.

Best regards,
George

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events