scriptrunner for creating scheduled issues

SaidAslan July 4, 2016

hi there.

have a ScriptRunner v.4.3.4 and JIRA 7.1.0. the task is to create a scheduled issues, one per week via Services. 

I tried the code below,

import com.atlassian.jira.component.ComponentAccessor

def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def constantsManager = ComponentAccessor.getConstantsManager()
def issueType = constantsManager.getAllIssueTypeObjects().find {it.name.equalsIgnoreCase("Incident")}
def issueInputParameters = issueService.newIssueInputParameters()

issueInputParameters.with {
    projectId = projectManager.getProjectObjByKey("SD").id
    summary = "Scheduled Issue"
    issueTypeId = issueType.getId()
    reporterId = "admin"
}

def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()

def issueResult = issueService.create(user, validationResult)
log.info "Issue created: ${issueResult.issue}"
return "Issue created: ${issueResult.issue}"

but if paste it in Script Console, the result is 

Issue created: null

can anyone explain me how to make it work? 

7 answers

1 accepted

2 votes
Answer accepted
Thanos Batagiannis _Adaptavist_
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.
July 4, 2016

this was a bit tricky, so the compete script (adding a value to customer request type will be) 

import com.atlassian.jira.component.ComponentAccessor

def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def requestTypeValue = "sdp/get-it-help" //replace it with the one you want - see note below

def constantsManager = ComponentAccessor.getConstantsManager()
def issueType = constantsManager.getAllIssueTypeObjects().find {it.name.equalsIgnoreCase("IT Help")}
def issueInputParameters = issueService.newIssueInputParameters()
def customFiledmanager = ComponentAccessor.getCustomFieldManager()
def cf = customFiledmanager.getCustomFieldObjectByName("Customer Request Type")

issueInputParameters.with {
    projectId = projectManager.getProjectObjByKey("SDP").id
    summary = "Issue created from script"
    issueTypeId = issueType.getId()
    reporterId = "admin"
}

issueInputParameters.addCustomFieldValue(cf.idAsLong, requestTypeValue)
def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()

def issueResult = issueService.create(user, validationResult)

assert !issueResult.errorCollection.hasAnyErrors()
return "Issue created: ${issueResult.issue}"

Note. In this example my customer request type key is "sdp/get-it-help". Probably this is not the same with the one you want. In order to find the correct one you can check in your instance by consulting the REST view of an issue: http://yourBaseUrl/rest/api/2/issue/yourIssueKey?expand=renderedFields,editmeta In this list you can find the value next to "customfield_10100" (id of the customer request type field). Assign the value you want in def requestTypeValue and you are done. 

Regards

Thanos

SaidAslan July 4, 2016

thanks a lot!!! 

it returned an error about Request type required again, so I changed it from customer request type to "request type" and it worked smile

the full code is: 

import com.atlassian.jira.component.ComponentAccessor
 
def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def constantsManager = ComponentAccessor.getConstantsManager()
def issueType = constantsManager.getAllIssueTypeObjects().find {it.name.equalsIgnoreCase("Service request")}
def issueInputParameters = issueService.newIssueInputParameters()
def customFiledmanager = ComponentAccessor.getCustomFieldManager()

def cf = customFiledmanager.getCustomFieldObjectByName("Request Type")
def cfcustreq = customFiledmanager.getCustomFieldObjectByName("Customer Request Type")

def requestTypeValue = "other-service-request" 
issueInputParameters.addCustomFieldValue(cf.idAsLong, requestTypeValue)
def custrequestTypeValue = "sd/other-service-request" 
issueInputParameters.addCustomFieldValue(cfcustreq.idAsLong, custrequestTypeValue) 

issueInputParameters.with {
    projectId = projectManager.getProjectObjByKey("SD").id
    summary = "Issue created from script"
    issueTypeId = issueType.getId()
    reporterId = "admin"
}
 
def validationResult = issueService.validateCreate(user, issueInputParameters)
assert !validationResult.errorCollection.hasAnyErrors()
 
def issueResult = issueService.create(user, validationResult)
 
assert !issueResult.errorCollection.hasAnyErrors()
return "Issue created: ${issueResult.issue}"
Like Mahesh Kallepalli likes this
Saida Aslanova July 12, 2016

Hi Thanos,

sorry for disturbing you again. 
I thought that this is a similar question so desided not to re-open the issue on your support page and write it right here. 

after adding the script .grv file in the Services (https://scriptrunner.adaptavist.com/latest/jira/services.html)  it returned the error: 

Errors: {pid=Anonymous users do not have permission to create issues in this project. Please try logging in first.}

besides this I found another errors in logs, one of them is Add a script root for this path. 

 

Thanos Batagiannis _Adaptavist_
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.
July 12, 2016

Hi Saida,

Try to replace 

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() wirth 

import com.atlassian.jira.component.ComponentAccessor
  
def issueService = ComponentAccessor.issueService
def projectManager = ComponentAccessor.projectManager
//get a user who can create issues by userKey
def user = ComponentAccessor.getUserManager().getUserByKey("admin")
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(user)
// rest of the code..

 

 

Mahesh Kallepalli August 27, 2019

@Thanos Batagiannis _Adaptavist_ @SaidAslan 

Can you please let me know where should we paste this code as i'm new to use script runner 

cgadade May 18, 2020

Hi @Thanos Batagiannis _Adaptavist_ ,

   I have a script which assigns the tickets to agents using round robin method .It is working fine on Workflow create transition but I want to run this script on every 15 min ,Can you please help to ,how to get mutable issue in scriptrunner [Custom job] scheduler  section and run my script.

Saida June 14, 2020

Hi @cgadade , 

You may write a JQL query to get these issues and try something like this: Running a jql query 

0 votes
SaidAslan July 4, 2016

image2016-7-4 16:15:53.png

forgot about request type at all! sorry smile 

I need to full the Request type field manually. can you explain how to edit it via groovy? it's a custom field with type [Extension for JIRA Service Desk] - Customer Request Type

0 votes
Thanos Batagiannis _Adaptavist_
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.
July 4, 2016

if you replace the 

assert issueResult.isValid() with assert !issueResult.errorCollection.hasAnyErrors()

do you get a more informative error message ?

0 votes
SaidAslan July 4, 2016

assert issueResult.isValid() | | | false com.atlassian.jira.bc.issue.IssueService$IssueResult@25a6496a

and error, 

0 votes
Thanos Batagiannis _Adaptavist_
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.
July 4, 2016

ok then, now if you add the following line 

//rest script...
def issueResult = issueService.create(user, validationResult)
log.debug(issueResult.errorCollection?.getErrors()) //<-- add this line in your script here
assert issueResult.isValid()
log.info "Issue created: ${issueResult.issue}"
return "Issue created: ${issueResult.issue}"
hopefully the logs will give us more information about the reason it failed 
0 votes
SaidAslan July 4, 2016

Hi Thanos,

it returns error:

assert issueResult.isValid() | | | false com.atlassian.jira.bc.issue.IssueService$IssueResult@374990a3

 - logs returned by service

0 votes
Thanos Batagiannis _Adaptavist_
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.
July 4, 2016

Hi Saida,

So you say that even in your log files you get an issue created null. Is there anything else there ? Apparently the issue failed to be created. Can you add the following line

//rest script ...
def issueResult = issueService.create(user, validationResult) 
assert issueResult.isValid() // <-- add this line in your script here
log.info "Issue created: ${issueResult.issue}"
return "Issue created: ${issueResult.issue}"

and rerun the script ?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events