Need solution for ScriptRunner post function "Create Subtask" and assign Subtask to user

Wayne Cranford May 9, 2018

A custom single picker custom field is being set as part of the transition to Create Subtask, but cannot get the Assignee to populate with the user's account - it populates instead with a string in the format of "username(username)".

I have tried numerous examples of the "set assignee" I found online, but no success.

I can hardcode the Assignee, but cannot get the variable aspect that is needed.  here is the last thing I tried:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Responsible Business Mgr"}
def implUser = sourceIssue.getCustomFieldValue(field1)

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

issue.assigneeId = implUser
issue.setReporter(currentAppUser);

The custom single-picker field, Responsible Business Mgr, the name "tcarwell" is entered, and what appears as the Assignee on the subtask is "tcarwell(tcarwell)"

All feedback welcomed,

Wayne

1 answer

1 accepted

0 votes
Answer accepted
JohnsonHoward
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.
May 10, 2018

Hi Wayne,

Are you putting this as a post-function? Where you are putting it in the order of post-functions can have an effect. Also so can if you are trying to update an issue outside of the one being transitioned.

If it is at the bottom or if you are trying to update a different issue to the one being transitioned, then you will need to update the database yourself in the code for the change to have an effect. In the example below I have shown how to do this.

If it is on the Create transition and at the top of the order of post-function, then you will need to be aware that the 'issue' has not yet technically been created, therefore the variable 'issue' will not yet be assigned. 

The example below was one I ran in the console, hence why line 13 where I have had to get the issue manually. This example also shows you how to get the custom field value of a user picker and turn it into a Application user so you can then use it to set the assignee.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager im = ComponentAccessor.getIssueManager()
Issue issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TEST-5")
def field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "User Picker"}
def user = issue.getCustomFieldValue(field1) as ApplicationUser
def userManager = ComponentAccessor.getUserManager()
issue.setAssignee(user)
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
im.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Hope this helps,

Johnson Howard (Adaptavist) 

Wayne Cranford May 10, 2018

Johnson - thank you for your response, and while the above did not operate as expected, I as able to take the above as input and found the following to work as needed:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager im = ComponentAccessor.getIssueManager()

def field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Responsible Business Mgr"}
def user = issue.getCustomFieldValue(field1) as ApplicationUser
def currentAppUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

issue.setAssignee(user)
issue.setReporter(currentAppUser);

To help fill in the rest of the story -we are using V7.4.4 of JIRA and v5.3.5 for ScriptRunner. This is a Script Post-Function on a workflow transition of an existing Parent Issue. I am using the "Create a sub-task" for the script.

The custom single-picker field "Responsible Business Mgr" is on the transition screen of the Parent Issue, for the workflow action that creates the Subtask. The above script now does the following:

  1. Enters the user selected the "Responsible Business Mgr" field on the Parent Issue;
  2. Populates this same field ("Responsible Business Mgr") on the Subtask;
  3. Assigns the newly create Subtask to the user in the "Responsible Business Mgr" field; and
  4. Updates the Reporter of the subtask to the person that executed the creation of the Subtask (instead of copying from the Parent)

As far as where in the order of Post Functions, I have it working as

  1. Set issue status to the linked status of the destination workflow step.
  2. Move up Move down Edit DeleteAdd a comment to an issue if one is entered during a transition.
  3. Move upMove down Edit DeleteScriptRunner workflow function - Create a sub-task (additional actions apply).

Subtask will be created with issue type: EOL Bus Mgr and summary Bus Mgr Subtask -.

It is also worked in the 5th location

  1. Set issue status to the linked status of the destination workflow step.
  2. Move up Move down Edit DeleteAdd a comment to an issue if one is entered during a transition.
  3. Move up Move down Edit DeleteUpdate change history for an issue and store the issue in the database.
  4. Move up Move down Edit DeleteRe-index an issue to keep indexes in sync with the database.
  5. Move upMove down Edit DeleteScriptRunner workflow function - Create a sub-task (additional actions apply).

Subtask will be created with issue type: EOL Bus Mgr and summary Bus Mgr Subtask -.

I am sure there are some best practices I am over looking, and I did place this script just before the re-index.

 

Thanks again for your assistance in resolving this,
Wayne

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events