Can't copy custom field to Assignee

Bruce Mohler September 20, 2019

I was trying to apply @JamieA's answer from https://community.atlassian.com/t5/Jira-questions/Why-can-I-not-set-assignee-by-ID-Script-Runner/qaq-p/87404 as we're trying to copy a string value from a custom field.

The custom field is called "Last Assignee" and is a string.

This is the relevant code (including the stuff from @JamieA's response.

def cfLastAssignee = customFieldManager.getCustomFieldObjectsByName("Last Assignee").first()
def String newAssignee = cfLastAssignee.getValue(issue)
def thisUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
def validateAssignResult = issueService.validateAssign(thisUser, issue.id, newAssignee)
issueService.assign(thisUser, validateAssignResult)

I'm sure I'm missing something simple.  All suggestions welcome.

When I run the code above, it returns:

2019-09-20 15:32:00,106 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: BRUCE-1, actionId: 1031, file: <inline script>
com.atlassian.jira.exception.DataAccessException: Error while retrieving user with name 'xcarter@gumo.net(xcarter@gumo.net)'.
	at com.atlassian.jira.issue.fields.AssigneeSystemField.getUser(AssigneeSystemField.java:407)
	at com.atlassian.jira.issue.fields.AssigneeSystemField.getValueFromParams(AssigneeSystemField.java:488)
	at com.atlassian.jira.issue.fields.AssigneeSystemField.updateIssue(AssigneeSystemField.java:311)
	at com.atlassian.jira.bc.issue.DefaultIssueService.assign(DefaultIssueService.java:561)
	at com.atlassian.jira.bc.issue.IssueService$assign$3.call(Unknown Source)
	at Script2455.run(Script2455.groovy:55)

2 answers

1 accepted

0 votes
Answer accepted
Mohamed Adel
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.
September 21, 2019

@Bruce Mohler 

are you sure you need to get list of customfield objects using "getCustomFieldObjectsByName".

if you need to get only one customField object you can use getCustomFieldObject(customfiedID) and no need in this case to use first()

also second line need to be updated as below

 

def cfLastAssignee = customFieldManager.getCustomFieldObject(customfiedID)
def newAssignee = cfLastAssignee.getValue(issue).toString()
Bruce Mohler September 24, 2019

@Mohamed Adel when I execute that last line, it returns null.  I would've expected it to return a string, no?

Mohamed Adel
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.
September 24, 2019

did you used the right customFieldId. it should be like this

def cfLastAssignee = customFieldManager.getCustomFieldObject(12003)
Bruce Mohler September 25, 2019

@Mohamed Adel Is there an equivalent method using the name of the custom field?  We have a dev environment and a prod environment and, wouldn't you know, the people who set them up ended up with different custom field numbers between them.

I tried getCustomFieldObjectByName("<customFieldName>") but it didn't like that.

Obviously, I'm struggling to figure out the libraries and interfaces and am a bit overwhelmed.

Mohamed Adel
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.
September 26, 2019

Unfortunately, getCustomFieldObjectByName() is deprecated and getCustomFieldObject() is safe for your case

Bruce Mohler September 26, 2019

I'd be convinced if there were an easy to tell which server I was on (dev or prod) and provide the correct field number for that server.

I wonder why they deprecated the more portable method?

Mohamed Adel
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.
September 26, 2019

it is their decision and I don't know the reason behind it.

regarding your issue, I found this error that can be your problem, can you give a try to the solution.

DataAccessException on Issues with Assignees that Have Been Deleted from Crowd

0 votes
Ilya Turov
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.
September 22, 2019

You are trying to turn ApplicationUser into a string, while you need to get that user's username:

String newAssignee = cfLastAssignee.getValue(issue)?.username

Because that's what validateAssign expects to see, but instead it gets this weird 'xcarter@gumo.net(xcarter@gumo.net)' string which is definitely not a username.

Bruce Mohler September 24, 2019

@Ilya Turov when I paste that line into the Post Function edit window, it doesn't recognize "username" as a valid property of cfLastAssignee.

I assume I'm doing something wrong.  Suggestions?

Ilya Turov
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.
September 24, 2019

suggestion is to ignore static type errors and still try running it :)

Suggest an answer

Log in or Sign up to answer