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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

Assigning fails using IssueService.assign() in workflow Edited

Hello and happy new year.

I am having an issue with doing issue assignments via IssueService.assign(ApplicationUser, IssueService.AssignValidationResult) using ScriptRunner via the Java API. Note that when I show my code, I have attempted to isolate this issue by creating a new project and doing some trivial assigns. This is not my actual production code but just an example extract.

This project is named DEMO and it’s a simple workflow just consisting of the statuses To Do and Done, with a single transition called Done. Both the create transition and the Done transition have the same workflow to set the assignee to the reporter:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def issueService = ComponentAccessor.getIssueService()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getUserManager().getUserByName("mishelpdesk")
def reporter = issue.getReporter().getName()
def validateAssignToReporterResult = issueService.validateAssign(user, issue.id, reporter)
if (validateAssignToReporterResult.isValid()) {
   issueService.assign(user, validateAssignToReporterResult)
}


In both cases, this script post-function is set as the last post-function in the transition. Here is what happens when I create a ticket with myself as the reporter, and then change the reporter back to the user mishelpdesk

Picture1.png

Picture2.png

Notice that in the issue history, the ticket is reassigned to mishelpdesk, but this does not persist or actually take place. However, if I execute the same function in the script console as opposed to the workflow, it works as intended.

Any idea why this would work in the create transition but not subsequent ones?

1 answer

1 accepted

Hi @Adam L ,

Happy 2021!

I didn't spot any obvious issue in your script.

It looks like the assignee is changed in the issue history but for some reason the re-index didn't happen for this issue.

  • Can you run a re-index in the project and see if the assignee changes to mishelpdesk? This will help us to understand if you are having an index issue. Reading the JAVA Doc, IssueService.assign() does re-index the issue after the update, and it's strange that it did work when you created the issue but it didn't work on your transition to Done.

Also, can you try the below script in your post-function? It's important that this script is placed before the re-index step instead of being on the last step as you did with your script.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser

def username = "mishelpdesk";

def user = ComponentAccessor.getUserManager().getUserByName(username)

issue.setAssignee(user)

 I didn't have the chance to test it, but I adapted this code from this script:  https://library.adaptavist.com/entity/round-robin-assign-issue-to-users-in-a-certain-project-role

I was trying deliberately to avoid issue.setAssignee() because I thought the only way to persist it was to use issue.store(), which is deprecated. However, I looked at the API and saw you could do this using issueManager().updateIssue(), where it works. This is an acceptable workaround but I am still curious why this issue exists for issueService. I'd like to keep it open for a couple days to see if there are any more insights.

Below is a short snippet of code I used:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def user = ComponentAccessor.getUserManager().getUserByName("mishelpdesk")
def reporter = issue.getReporter()
issue.setAssignee(reporter)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_ASSIGNED, true)

I hope someone from community can explain why IssueService didn't work :)

I do agree with you, issue.store() must be avoided since there are known issues about this

I think if you place this code in the first position of the post-functions, you won't need to use issueManager.updateIssue(), my assumption is that Scriptrunner will modify the MutableIssue on runtime and pass the modified issue to the next post-function.

 

Can you try your script without the last line in the first post-function position and see if that works?

Yes, this worked for me, thanks! Before I created this ticket I tried adjusting the order and it did not fix via the IssueService method but it does when you call setAssignee on MutableIssue. I will mark this as the accepted answer for now but I am still curious why this is happening.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.4
TAGS
Community showcase
Published in Jira

Announcing the waitlist for Jira Work Management

Hey there Cloud Community members! We’re excited to give you the first glimpse of the new home for business teams on Jira — Jira Work Management. Jira Work Management is the next generation of J...

880 views 14 20
Read article

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