Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,368,414
Community Members
 
Community Events
168
Community Groups

Groovy Script NPE error when updating multi-user custom field in a post function

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?

1 answer

1 accepted

0 votes
Answer accepted

Hi @Atlassian Admin _Zynga_ ,

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 [Tecnofor] . This script worked fine without any errors. Could you please clarify on the issue with the original script?

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

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events