Script Runner set assignee in a post function

MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2014

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

2 votes
MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2014

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2014

:-)

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

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2014

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.

MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 3, 2014

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

CCP TechOps July 23, 2014

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

JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 23, 2014

No information there.

Roy Chapman November 15, 2017

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
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 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
Roy Chapman November 16, 2017

<Embarrassed face>

Ramesh Udari1 January 28, 2018

Is this issue resolve ?.

 

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

Roy Chapman January 29, 2018

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
TAGS
AUG Leaders

Atlassian Community Events