Degraded performance Customers may experience issues using Community search. Our team is investigating.
It's not the same without you
Join the community to find out what other Atlassian users are discussing, debating and creating.
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?
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
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 ?
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}"
image2016-7-4 16:15:53.png
forgot about request type at all! sorry
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
This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.
Read moreAs a Jira power user, I was at first doubtful that Trello could benefit my workflow. Jira already uses boards (ones you can customize!), so why would I even need to use Trello?! In this post you will...
Connect with like-minded Atlassian users at free events near you!
Find a groupConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no AUG chapters near you at the moment.
Start an AUGYou're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.