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)
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()
@Mohamed Adel when I execute that last line, it returns null. I would've expected it to return a string, no?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
did you used the right customFieldId. it should be like this
def cfLastAssignee = customFieldManager.getCustomFieldObject(12003)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Unfortunately, getCustomFieldObjectByName() is deprecated and getCustomFieldObject() is safe for your case
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
suggestion is to ignore static type errors and still try running it :)
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.