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

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

2 votes
Answer 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}"
Like Mahesh Kallepalli likes this

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

 

 

@Thanos Batagiannis _Adaptavist_ @Saida Aslanova 

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

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.

Hi @cgadade , 

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

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
TAGS
Community showcase
Asked in Jira Service Management

JSM June ask me anything (AMA)

Hello Community members! We’re wrapping up the end of JSM June with an Ask Me Anything (AMA) with the Jira Service Management product team. This is your chance to ask all your ITSM questions to o...

262 views 13 14
View question

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