Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Create subtask with script runner in service management project as a post function

Kántor Tibor August 17, 2022

Hi Community!

I am trying to create subtasks for an issue, in a Service Management project as a post function during create transition. It is based on a multi select custom field, so that one subtask is to be created for each selected item.

However the script runs into a permission error when a user outside of the service management group is trying to create the issue.

Heres the simplified script:

 

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.fields.config.manager.PrioritySchemeManager

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.event.type.EventDispatchOption

import com.onresolve.scriptrunner.runner.customisers.PluginModule

import com.onresolve.scriptrunner.runner.customisers.WithPlugin

//get technical user

def techUser = ComponentAccessor.getUserManager().getUserByName("techuser")

 

// the priority of the new issue

def priorityName = issue.priority.getName()

 

def issueService = ComponentAccessor.getIssueService()

def constantsManager = ComponentAccessor.constantsManager

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def prioritySchemeManager = ComponentAccessor.getComponent(PrioritySchemeManager)

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def parentIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(issue.key)

 

def subTaskIssueType = constantsManager.getIssueType('11101')

//set reporter as reporter of sub-tasks

def reporter = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def priorityId = constantsManager.priorities.findByName(priorityName)?.id

    String subSummary = "subSummary"

    String subDescription = "subDescription"

    def issueInputParameters = issueService.newIssueInputParameters()

        issueInputParameters.setSkipScreenCheck(true)

        issueInputParameters.setProjectId(parentIssue.projectObject.id)

        issueInputParameters.setIssueTypeId(subTaskIssueType.id)

        issueInputParameters.setReporterId(reporter.username)

        issueInputParameters.setSummary(subSummary)

        issueInputParameters.setPriorityId(priorityId)

        issueInputParameters.setDescription(subDescription)

     

    def validationResult = issueService.validateSubTaskCreate(techUser, parentIssue.id, issueInputParameters)

    assert validationResult.valid : validationResult.errorCollection

 

    def issueResult = issueService.create(techUser, validationResult)

    assert issueResult.valid : issueResult.errorCollection

 

 //set issue parent - sub-task link

    def subtask = issueResult.issue

    ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, subtask, techUser)
When it runs it throws the following error:
2022-08-18 00:07:02,578 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue ITSD-6821 for user 'test_user'. View here: https://jira.mycompany.com/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=ITSD%3A+Test+workflow+for+JIRA+Service+Desk&descriptorTab=postfunctions&workflowTransition=1&highlight=7 java.lang.AssertionError: Errors: {} Error Messages: [You do not have permission to assign issues.]. Expression: validationResult.valid
What am I missing?
Any help would be appreciated!
Thanks

2 answers

1 accepted

4 votes
Answer accepted
Joseph Chung Yin
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 17, 2022

@Kántor Tibor -

In addition to what @Nic Brough -Adaptavist- stated - Another thing for you to consider is not assigning the issue.

Best, Joseph Chung Yin

Jira/JSM Functional Lead, Global Infrastructure Applications Team

Viasat Inc.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 17, 2022

Thanks Joseph!

I forget this option a lot, I usually work with Jira systems that globally require someone to own each issue (it's a good practice to have a single point of contact/whinging/failure), and hence can't leave the assignee blank.

But no assignee would also solve your problem.  As long as you tell your people that they need to be looking at them as well as what they are assigned to.

Kántor Tibor August 18, 2022

Thanks @Nic Brough -Adaptavist- and @Joseph Chung Yin for your answers. The techuser is an agent so that shouldn't be a problem. Also I don't see where the assigning happens with this script. There is no issueinputparameter for an assignee. However I also tried to include it with null value to handle it by JIRA automatically when creating issues manually, but the result is still the same. So what did you mean by trying to not assign the issue in this case?

Thanks!

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 18, 2022

I suspect you have set the assignee mandatory, or set the "do not allow unassigned issues", so the project requires an assignee, but the test user does not have "assign issue" permission.

Kántor Tibor August 18, 2022

If I create the issue with an agent it stays unassigned, so that's what bothers me, that it is allowed to be unassigned.

Anyway, in the meantime I found a "solution":

ApplicationUser technicalUser = ComponentAccessor.getUserManager().getUserByName("techuser")
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(technicalUser)
def techUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

This sets the loggedinuser to the techuser which is an agent, so the permission problem does not exist anymore.

Thanks for your answers!

2 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 17, 2022

Your code looks fine to me, I think it's what it mentions in the error message - test_user does not have "assign issues" permission in the project.

As a JSM project, it might not be just that - I cannot remember the exact rules, but in JSM projects, only Agents can do some things, and I think assigning issues may be one of them.

If that is the case, then you have three options:

  • Make test_user into an Agent
  • Write extra code to bypass the permissions
  • Use a different user, one that does have the right profile

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.10
TAGS
AUG Leaders

Atlassian Community Events