Post script function - multiple user picker custom field update

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

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)
}

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

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

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).
 

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")

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

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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published yesterday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

39 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you