Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I append a current assignee ApplicationUser to an ArrayList to store in a Single User Select?

Dylan
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 7, 2019

I am working on a project where I want to have a "Previous Assignees" Single User Select custom field that, at certain points of the workflow, will get the current assignee's ApplicationUser object appended to it. I'm currently trying to get it to work with an ArrayList, but with no luck. Is there a better data structure to go about doing this?

I'm trying to implement a function that will allow the user to go to a screen where there will be the Single User Select field that they can select from the users that have been previously selected. 

It seems like the data is not persisting in the custom between transitions. For example, it will begin the script as having null in it and then at the end it will have the assignee's ApplicationUser object. Then, when the script runs on a different transition within the same issue, the custom value has null at the beginning of the script and then it gets the new assignee placed in it. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.spi.Step
import java.text.SimpleDateFormat
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.user.ApplicationUser;

MutableIssue myIssue = issue
// Saving current assignee
def assignee = myIssue.getAssignee()

// Logs the current assignee's display name
log.warn(assignee.displayName)

// Creates instance of CustomFieldManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

// Gets the "Previous Assignee Picker" Single User Select Custom Field Object
def PreviousAssignees = customFieldManager.getCustomFieldObjectByName("Previous Assignee Picker")

// Gets the value out of the field
def cfSingleUserSelectValue = myIssue.getCustomFieldValue(PreviousAssignees)
log.warn("Previous Assignee Picker Value Before: ${cfSingleUserSelectValue}")

// Declare new empty ArrayList called newAssignees
List<ApplicationUser> newAssignees = [];

// If the User Select custom field is NOT empty
if (cfSingleUserSelectValue) {

// Put values of the previous custom field's (the ArrayList) into the newAssignees variable
newAssignees = (cfSingleUserSelectValue) as List<ApplicationUser>

// Add the new assignee to the ArrayList
newAssignees.add(assignee)
}

// If the User Select custom field IS empty
else
// Just add the new assignee to the ArrayList
newAssignees.add(assignee);

// Set the "Previous Assignee Picker" custom field value to the newly appended ArrayList
myIssue.setCustomFieldValue(PreviousAssignees, newAssignees)
// Pulls the value out for logging purposes
cfSingleUserSelectValue = myIssue.getCustomFieldValue(PreviousAssignees)
log.warn("Previous Assignee Picker Value After: ${cfSingleUserSelectValue}")

 

1 answer

0 votes
PD Sheehan
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 7, 2019

The "setCustomFielValue" method for a single user picker just updates the selected value.

You could use a multi user picker to store previously assigned users. This field type stores an array of users as its value.

But in both cases, the data source is always the full list of users that the current user has access to. I am not sure if the scriptrunner behaviour method formField.setFieldOptions() works on user picker fields. You could try it. 

If it works, you would use the data in the multi user picker to limit the data source on the single user picker.

 

Another option for data source, instead of using the multi user picker would be to use IssuePropertyService. But this doesn't get indexed and can't be searched like the multiUserPicker. 

import com.atlassian.jira.bc.issue.properties.IssuePropertyService
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
def issuePropertyService = ComponentAccessor.getComponentOfType(IssuePropertyService.class)
def previousAssignees = new JsonSlurper().parseText(issuePropertyService.getProperty(null, issue.id, 'previousAssignees') )
// your code append to previous assignee
issuePropertyService.setProperty(null, issue.id, 'previousAssignees', new JsonBuilder(previousAssignee).toString())

 

Suggest an answer

Log in or Sign up to answer