scriptrunner for creating scheduled issues

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

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

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}"

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. 

 

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..

 

 

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 ?

Hi Thanos,

it returns error:

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

 - logs returned by service

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 

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

and error, 

if you replace the 

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

do you get a more informative error message ?

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

139 views 0 3
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you