You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
I have the below script to update a multi-user custom field in a post function.
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.IssueChangeHolder;
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
IssueManager issueManager = ComponentAccessor.getIssueManager();
MutableIssue issue = issueManager.getIssueObject(issue.id);
def subTasks = issue.getSubTaskObjects()
List<ApplicationUser> users;
CustomField multiUser = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("CC List")
subTasks.each {
String assignee1 = it.assigneeId
if(users == null)
users = new ArrayList<>();
users.add(ComponentAccessor.getUserManager().getUserByName(assignee1))
}
issue.setCustomFieldValue(multiUser, users);
issueManager.updateIssue(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue, EventDispatchOption.ISSUE_UPDATED, false)
The field gets updated in the issue after adding the last line, but also seeing the below error in the logs.
2019-02-09 13:43:59,780 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2019-02-09 13:43:59,781 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: FA-635, actionId: 51, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.customfields.impl.MultiUserCFType.getChangelogValue(MultiUserCFType.java:123) at com.atlassian.jira.issue.customfields.impl.MultiUserCFType.getChangelogValue(MultiUserCFType.java:80) at com.atlassian.jira.issue.fields.ImmutableCustomField.getChangelogValue(ImmutableCustomField.java:376) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:411) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669) at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655) at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214) at com.atlassian.jira.issue.IssueManager$updateIssue$2.call(Unknown Source) at Script350.run(Script350.groovy:32)
Could someone help resolve the issue?
You could use something like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField multiUser = customFieldManager.getCustomFieldObjectByName("CC List")
ArrayList<ApplicationUser> users = []
issue.getSubTaskObjects()?.each {
if (it.getAssignee()) users.add(it.getAssignee())
}
if (users) {
issue.setCustomFieldValue(multiUser, users)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
Thanks @Alejandro Suárez . This script worked fine without any errors. Could you please clarify on the issue with the original script?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Sure, the initial problem with your code is that you are getting those exceptions because in some subtasks the assignee field is null. You have to check that field if you dont want to log null exceptions.
And you complex more than necesary your code, for example, you are cheking if the users variable is null when it allways will be null, and you are using a method from userManager to get an ApplicationUser, when issue.getAssignee() method gives you an ApplicationUser itself.
Regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.