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

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

2 votes

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 Feb 03, 2014

:-)

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

JamieA Feb 03, 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.

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

CCP TechOps Jul 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 Jul 23, 2014

No information there.

Roy Chapman Nov 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 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
Roy Chapman Nov 16, 2017

<Embarrassed face>

Ramesh Udari Jan 28, 2018

Is this issue resolve ?.

 

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

Roy Chapman Jan 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
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Join our webinar: “Asset Management: The Role of Context in ITSM” with Atlassian & Riada

  Hi Atlassian Community! This is Teresa from the Jira Service Desk team. On June 19th, I’ll be hosting a webinar “Asset Management: The Role of Context in ITSM” with Atlassian Partne...

177 views 2 3
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