Scripts works fine in ScriptRunner - Fails in Groovy

Ruben August 20, 2019

Hey All,

The script below returns a list of Organizations attached to a Service Desk Project. 

The script works perfectly fine within scriptrunner, however fails to run within Groovy. 

The Error that I get is:

Message:
java.lang.NullPointerException: Cannot invoke method getOrganizations() on null object
Stack:
org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:91)
org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:43)
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
org.codehaus.groovy.runtime.callsite.NullCallSite.call(NullCallSite.java:34)
org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135)
script1566299369126730399913.run(script1566299369126730399913.groovy:47)

The script is as follows:

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.servicedesk.api.ServiceDeskManager
import com.atlassian.servicedesk.api.organization.OrganizationsQuery
import com.atlassian.servicedesk.api.organization.OrganizationService
import com.atlassian.servicedesk.api.util.paging.SimplePagedRequest
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequest
import com.atlassian.servicedesk.api.util.paging.LimitedPagedRequestImpl
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

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

@WithPlugin("com.atlassian.servicedesk")

@PluginModule
ServiceDeskManager serviceDeskManager

@PluginModule
OrganizationService organizationService


def serviceDeskId = 16 as Integer

// get the available organizations for that project
def organizationsQuery = new OrganizationsQuery() {
@Override
Optional serviceDeskId() {
Optional opt = Optional.of(serviceDeskId);
return opt
}

@Override
LimitedPagedRequest pagedRequest() {
return new LimitedPagedRequestImpl(0, 50, 100)
}
}


// get all the organizations configured for that project
log.info((String)currentUser)
log.info((String)organizationsQuery)
def organizationsToAdd = organizationService.getOrganizations(currentUser, organizationsQuery)?.toList()
log.error(organizationsToAdd.getClass())
return organizationsToAdd

currentUser returns the logged in user(me)

organizationsQuery returns:  

script1566299369126730399913$1@ad6a688

Does anyone have an idea how i can get around this error, or what i'm doing wrong? 

Thanks in adviced,  

2 answers

1 accepted

3 votes
Answer accepted
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 20, 2019

Hi Ruben,
the @WithPlugin and @PluginModule annotations are specific to ScriptRunner. They are likely not showing as errors because you also have ScriptRunner installed on your Jira instance.
The problem with your script is that you need to initialize the serviceDeskManager and organizationServicevariables (which in ScriptRunner are initialized by @PluginModule). This is done using the getComponent global function:

ServiceDeskManager serviceDeskManager = getComponent(ServiceDeskManager)
OrganizationService organizationService = getComponent(OrganizationService)
Ruben August 21, 2019

Awesome, that was exactly it, thanks alot David :) 

ghaith alrai
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 20, 2020

Thanks David a lot :)

0 votes
Lady Di
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
August 20, 2019
Ruben August 20, 2019

no sorry JMWE Groovy script

Suggest an answer

Log in or Sign up to answer