Post script function - multiple user picker custom field update

IT Accounts April 28, 2016

Can't update multiple user picker custom field. 

Workflow transition's post function script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.getUserManager()
def Approvers = customFieldManager.getCustomFieldObjectByName("MultipleUserPicker")
List<applicationuser> newApprovers = []; 
ApplicationUser user = userManager.getUserByKey("user1"); 
newApprovers.add(user); 
user = userManager.getUserByKey("user2"); 
newApprovers.add(user);
issue.setCustomFieldValue(Approvers, newApprovers) 
issue.store();

This code gives no mistake in text editor, but when I performing transition it alerts:
It seems that you have tried to perform an illegal workflow operation.If you think this message is wrong, please contact your JIRA administrators.

I can't see logs. Does anybody know what is wrong? When I comment this line "issue.setCustomFieldValue(Approvers, newApprovers)" alert disappears.

Here on this page is example only for (single select) user picker. 
Thank You!

2 answers

1 accepted

1 vote
Answer accepted
adammarkham
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.
April 28, 2016

You should use IssueService, which will do the reindexing for you. You should be able to replace your script with the one below:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueService = ComponentAccessor.getComponent(IssueService)
def approvers = customFieldManager.getCustomFieldObjectByName("MultipleUserPicker")

def issue = issue as Issue

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def issueInputParameters = issueService.newIssueInputParameters()

// Adding the user names here
issueInputParameters.addCustomFieldValue(approvers.id, "user1", "user2")

def updateValidationResult = issueService.validateUpdate(user, issue.id, issueInputParameters)

if (updateValidationResult.isValid()) {
    issueService.update(user, updateValidationResult)
}
IT Accounts April 28, 2016

I changed names of CustomField and users but it is not working.
This script generates no error, but field is empty. =((

IT Accounts April 28, 2016

I think I know what happened, user must have a permission to edit? I'll check it...

IT Accounts April 29, 2016

Is it possible to select another user, not current logged in? 
"Edit issue" is deprecated in our project, we are changing fields in transitions. Each field in its own transition (very strict).
 

adammarkham
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.
April 29, 2016

Yes if the logged in user does not have issue edit permissions it can't be done. You should run it as a user with the correct permissions. You can get a user like so:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager

def userManager = ComponentAccessor.getComponent(UserManager)

def user = userManager.getUserByKey("userkey")
Thomas Scheibelhofer November 30, 2017

Hi Adam,

I'm trying to do the same, but what i have is an array / list of users (= MemberUsernames) of which i do not know how many they will be.

Do you have a suggestion on how i could do that?

issueInputParameters.addCustomFieldValue(approvers.id, MemberUsernames)

def updateValidationResult = issueService.validateUpdate(currentReporterFull, issue.id, issueInputParameters)

if (updateValidationResult.isValid()) {
issueService.update(currentReporterFull, updateValidationResult)
}
}

Thanks and best regards

Thomas

0 votes
IT Accounts April 28, 2016

I changed names of CustomField and users but it is not working.
This script generates no error, but field is empty.

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events