Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

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

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

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

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

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

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
Community showcase
Published in Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

222 views 2 1
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