Forums

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

Scriptrunner - Adding organizations when a Service Desk issue gets created

Hugo Noronha October 25, 2018

Hi,

I'm trying to use this sample script to add organization on create but I'm having compiling issues

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())

 

The compiling issues are related with two lines of code

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

2.  def organizationsToAdd = organizationService.getOrganizations(currentUser, organizationsQuery)?.right()?.get()?.results

Both return the error (No such property id for class: R or No such property results for class: R)

If I try to log serviceDeskProject?.right()?.get() I get the result below:

I want to get the value 3 from serviceDeskId but I'm unable to do it. I did already try 

serviceDeskProject?.right()?.get()?.getId as Integer and serviceDeskProject?.right()?.get()?.serviceDeskId as Integer but I still get the same compilation error (No such property id for class: R or No such property results for class: R)

serviceDeskId :Either.Right(ServiceDeskImpl{serviceDeskId=3, projectId=12502, projectName=ITSM TEST, accessConfig=AccessConfig{publicSignUp=false, openAccess=true}, createdByUserKey=some(afr), createdDate=some(2018-03-19 11:32:11.095), createdWithEmptyProject=some(true), createdAtVersion=some(3.11.0-REL-0005), legacyCommentTransitionDisabled=true})

 

 

2 answers

0 votes
Hugo Noronha October 29, 2018

Hi,

I get this when I execute it in a workflow

2018-10-29 10:20:34,222 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-10-29 10:20:34,222 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ITSM-119, actionId: 1, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.servicedesk.internal.feature.organization.api.OrganizationServiceImpl.getOrganizations(OrganizationServiceImpl.java:156)
 at com.atlassian.servicedesk.api.organization.OrganizationService$getOrganizations$0.call(Unknown Source)
 at Script22.run(Script22.groovy:40)
Gezim Shehu [Communardo]
Community Champion
October 29, 2018

Im not sure.

I'll try and check again at the upgraded instance I have access

Dhavval Aarya
Contributor
May 31, 2019

Hi @Gezim Shehu [Communardo],

Thank you for sharing your script and I am running this script on JSD 4.0.2 and Jira 8.0.2 as "Script listner" in create issue event and I am getting following error. I am new to scriptrunner and JIRA and trying to get organizations field from the email address of reporting user using customer portal.

Any help will be really appreciated. Thank you in advance.

2019-05-29 11:56:55,100 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-05-29 11:56:55,100 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: TCH-31, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.servicedesk.internal.feature.servicedesk.ServiceDeskImpl.isLeft() is applicable for argument types: () values: []
Possible solutions: inspect(), inject(groovy.lang.Closure), inject(java.lang.Object, groovy.lang.Closure), sleep(long), sleep(long, groovy.lang.Closure), isCase(java.lang.Object)
 at Script474.run(Script474.groovy:28)

 

Gezim Shehu [Communardo]
Community Champion
June 3, 2019

Hi @Dhavval Aarya ,

 

Not sure if there are any changes on the . I'm currently on 7.12.3 and seems to be working fine for me.

Could you try removing that block and see if it runs?

 

Br

Dhavval Aarya
Contributor
June 3, 2019

Hello @Gezim Shehu [Communardo] 

Thank you for your reply. now I am getting following error.

2019-05-29 11:40:58,021 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-05-29 11:40:58,022 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script457.groovy: 38: unexpected token: Option @ line 38, column 5.
       Option&lt;Integer&gt; serviceDeskId() {
       ^
1 error
image.png
Gezim Shehu [Communardo]
Community Champion
June 3, 2019

Kindly replace with < and > instead of &lt or &gt

Dhavval Aarya
Contributor
June 3, 2019

Hello @Gezim Shehu [Communardo] ,

Thank you for your time for helping me on this. I have tried your script on both workflow scriptrunner post function and scriptrunner script listner on create issue event and I am getting error on both places. 

is there any way I can directly send you an email and stop spamming this place and once solution is found, I can post it here?

Best Regards,

Dhavval Aarya

Gezim Shehu [Communardo]
Community Champion
June 5, 2019

Hi @Dhavval Aarya ,

 

Please try it on the script console and try debugging it on where it fails.

For script console you can use something like this to help retrieve the issue

 

 

import com.atlassian.jira.component.ComponentAccessordef issue = ComponentAccessor.getIssueManager().getIssueObject('ABC-123')

 


While debugging can be done by simply logging, at least seeing until when your script executes.

 

log.warn('test')
Like Dhavval Aarya likes this
Dhavval Aarya
Contributor
June 6, 2019

Thank you @Gezim Shehu [Communardo] for your reply, after enabling log.warn, i am getting following information with the error:

2019-06-06 13:48:43,546 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-06-06 13:48:43,546 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script47.groovy: 51: The return type of com.atlassian.fugue.Option serviceDeskId() in Script47$1 is incompatible with java.util.Optional in com.atlassian.servicedesk.api.organization.OrganizationsQuery
. At [51:5]  @ line 51, column 5.
       @Override
       ^
1 error
Gezim Shehu [Communardo]
Community Champion
June 10, 2019

Can you please share the entire script how you are using it?

My guess there were api changes going from 7.x to 8.x. However I'm still on 7.x so i need to check the script first.

Dhavval Aarya
Contributor
June 11, 2019

As you've mentioned above to use your script in post function of workflow at last step of create transition is as below and also the error message.

:

Time (on server): Tue Jun 11 2019 10:12:35 GMT+0200 (Central European Summer Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2019-06-11 10:12:35,178 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2019-06-11 10:12:35,186 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: TCH-40, actionId: 1, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.servicedesk.internal.feature.servicedesk.ServiceDeskImpl.isLeft() is applicable for argument types: () values: []
Possible solutions: inspect(), inject(groovy.lang.Closure), inject(java.lang.Object, groovy.lang.Closure), sleep(long), sleep(long, groovy.lang.Closure), use([Ljava.lang.Object;)
 at Script48.run(Script48.groovy:28)

image.png

0 votes
Gezim Shehu [Communardo]
Community Champion
October 25, 2018

Below is a script I use to add an organization on create issue/ticket

 

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.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

import org.apache.log4j.Level
log.setLevel(Level.INFO)

@WithPlugin("com.atlassian.servicedesk")

@PluginModule
ServiceDeskManager serviceDeskManager

@PluginModule
OrganizationService organizationService

MutableIssue issue = issue

def sdUser = ComponentAccessor.getUserManager().getUserByKey('robotuser')
def serviceDeskProject = serviceDeskManager.getServiceDeskForProject(issue.projectObject)

// if the project is not Service Desk type, return
if (serviceDeskProject.isLeft()) {
    log.error "${serviceDeskProject?.left()?.get()}"
    return
}

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

//Build Org Query
def organizationsQuery = organizationService.newOrganizationsQueryBuilder().serviceDeskId().build()

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

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName('Organizations')
def existing = cf.getValue(issue)

for (organization in organizationsToAdd){    
    
    if (organization.toString() == 'Organization Name goes here'){       
        
        if(!existing.isEmpty()){
            
            if(!existing.contains(organization)){      
               existing.add(organization)
               cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), existing), new DefaultIssueChangeHolder())
               log.info("Organization Added!")
            }
            else if(existing.contains(organization)){
                   log.info("Organization Already Exists!")
                   return          
               }
        }
        else{
           cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), [organization]), new DefaultIssueChangeHolder())
           log.info("Organization Set!")
           }                 
    }    
}
//x
Gezim Shehu [Communardo]
Community Champion
October 25, 2018

@Hugo Noronhayou have mistakely inserted your dev url I think at the end of the post

 

"Cancel"

Hugo Noronha October 25, 2018

Thank you Gezim

Hugo Noronha October 25, 2018
//get SD ID
def serviceDeskId = serviceDeskProject?.right()?.get()?.id as Integer

It is in this line of code that I get the compiling error No such property id for class: R. Trying your script I get the same error

Gezim Shehu [Communardo]
Community Champion
October 25, 2018

Did you try running the script in a post function ? I pulled it out of a functional workflow. Should be ok, despite the warnings.

 

Just put it in the last order

Hugo Noronha October 25, 2018

Hi Gezim,

Yes I did run it on a scriptrunner postfunction. Maybe we are running different ServiceDesk API Versions?

I'm running JIRA Service Desk - 3.14.0-REL-0006

Gezim Shehu [Communardo]
Community Champion
October 26, 2018

Im on JIRA 7.3.0 and SD 3.3.0.

But I have a dev instance currently upgrading. Let me check there later

Gezim Shehu [Communardo]
Community Champion
October 26, 2018

@Hugo NoronhaI tried and you are right it didn't work.

However debugging quickly i found that the output of organization.toString() now has changed so i replaced it instead with organization.name

 

Now it works

image.png

Hugo Noronha October 26, 2018

Hi Gezim,

And what about 

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

 According to what I read, it's only working with Service Desk 3.3.0 API.  What were the changes that you made on the above line of code for your script to work?

Thanks in advance :)

Gezim Shehu [Communardo]
Community Champion
October 26, 2018

I did not make changes to that.

It worked just by the one line fix :)

Hugo Noronha October 26, 2018

:) Ok, I'm reindexing my instance. I'll feedback results as soon as I test your solution 

Cheers

Hugo Noronha October 29, 2018

Hi Gezim,

I still get a lot of compilation errors

Capture.JPG

So it does not compile. Any thoughts?

Gezim Shehu [Communardo]
Community Champion
October 29, 2018

Hi,

 

I also get errors. These however are script runners type error/warning.

In my instance it executes fine.

 

Have you tested in a workflow?

 

Br

Suggest an answer

Log in or Sign up to answer