Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

552 views 15 18
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you