Script Runner set assignee in a post function

I must be missing something in this simple script. It's a post function (not in the Create transition). The logs appear just as expected, the user in question can be assigned the issue via the UI, the custom field exists. But the assignee is never changed in the issue.

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.issue.MutableIssue

/**
 * The template script for setting the assignee from a custom user picker
 * Make sure the user chosen is assignable.
 */

def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");
log.setLevel(org.apache.log4j.Level.DEBUG);
 
def issueManager = ComponentAccessor.getIssueManager();
def customFieldManager = componentManager.getCustomFieldManager();
def cf = customFieldManager.getCustomFieldObjectByName("Source User");
if (cf == null) {
    log.error("No custom field found");
    return;
}
def user = issue.getCustomFieldValue(cf);
if (user != null) {
    def user2 = user.getDirectoryUser();
    log.error("Setting assignee to: " + user2.getName());
    log.error("Issue type : " + issue.getClass().getName());
    MutableIssue mutableIssue = issueManager.getIssueObject(issue.id)
    mutableIssue.setAssignee(user2);
    mutableIssue.store();
}

I've also tried using

MutableIssue myIssue = issue

1 answer

This widget could not be displayed.

The script works fine. The problem was that the workflow I was testing it with had another post function lower down that set the assignee to the current user. I should take my own advice and stay away from the default JIRA workflows and the unexpected stuff that lurks in them :-)

Henning Tietgens Community Champion Feb 03, 2014

:-)

I would stay with MutableIssue myIssue = issue, because otherwise you may get index problems.

You can just reference "issue" without declaring it... I tend to use the construct Henning posted only because then you get typing help from the IDE.

Agree with not reloading the issue though... this will reload the issue from the db and disregard what the user just entered on the form - depending on the ordering of the post-functions.

Got it. I forgot that IssueImpl is already a MutableIssue.

I tried above script and get below error. Please let me know how to resolve it and the proper syntax as I am new to Groovy.

2014-07-24 00:16:30,371 http-17000-14 ERROR ikkhan 16x14831x1 j02hwu 10.19.0.17,10.253.96.38 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.PostFunction] Issue type : com.atlassian.jira.issue.IssueImpl

No information there.

I am trying to use the same script but I'm seeing this line gives an error

MutableIssue myIssue = issue

The script could not be compiled:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script227.groovy: 29: unable to resolve class MutableIssue 
 @ line 29, column 14.
   MutableIssue myIssue = issue

Anything obvious?

Henning Tietgens Community Champion Nov 15, 2017

Maybe you forgot to copy the import statements in the script? Regarding your error message the following line is missing

import com.atlassian.jira.issue.MutableIssue

<Embarrassed face>

Is this issue resolve ?.

 

Please post correct script for it, we are also facing same issue

After much playing around we found existing functionality from "Assign From Field This function assigns issue to user from the custom field.".  Part of the script runner, so no need for custom scripting

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

512 views 3 18
Read article

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