Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Copying value assignee field in custom field

Alexander
Contributor
September 21, 2018

Hi!
I try run this script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser

MutableIssue issue
Issue issueIM  = issue   
CustomField technolog = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("name customfield")


def value_a = technolog.getValue(issue)
def assignee = issue.getAssigneeId()


if(value_a == null ||value_a == ""){
    technolog.updateValue(null, issue, new ModifiedValue(null, assignee), new DefaultIssueChangeHolder())
}
else {
 issue.setAssignee((ApplicationUser)value_a)
}

We need the assignee field value to be copied to the custom field in case the custom field is empty.
But script don't work.

Regard

2 answers

1 accepted

2 votes
Answer accepted
Tarun Sapra
Community Champion
September 21, 2018

Hello @Alexander

This is not right in your code

def value_a = technolog.getValue(issue)

To get the customfield value you should use issue.getCustomFieldValue 

def value_a = issue.getCustomFieldValue(technolog)

Also, to update issues you should always use IssueService

https://developer.atlassian.com/server/jira/platform/performing-issue-operations/

Alexander
Contributor
September 23, 2018

Hi @Tarun Sapra
Thanks for your reply
If a custom field has a value, the Assignee field accepts it.
However, the Assignee field does not take a custom field value.
I guess error in this line:
 technolog.updateValue(null, issue, new ModifiedValue(null, assignee), new DefaultIssueChangeHolder())
Error log:
java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.jira.user.ApplicationUser at com.atlassian.jira.issue.customfields.impl.UserCFType.getDbValueFromObject(UserCFType.java:83) at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:138) at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396) at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source) at Script1176.run(Script1176.groovy:20)

Sorry, I'm new to this.

Regards

Tarun Sapra
Community Champion
September 24, 2018

Hello @Alexander

What's the type of the field "technolog" in jira, is it text or single select etc

Alexander
Contributor
September 24, 2018

Type is "User Picker (single user)"

Tarun Sapra
Community Champion
September 24, 2018

Then you should save the ApplicationUser object instead of string in the custom field updateValue.

Alexander
Contributor
September 24, 2018

I want it:
Copy the user from the Assignee field to the custom field "technolog"  (replacing value).
This method does not work :
 technolog.updateValue(null, issue, new ModifiedValue(null, assignee), new DefaultIssueChangeHolder())
Help edit please...

Alexander
Contributor
September 24, 2018

"Then you should save the ApplicationUser object instead of string in the custom field updateValue."- Yes, it's right!

Tarun Sapra
Community Champion
September 24, 2018

Glad to know @Alexander that it works, please accept/upvote the answer so that others are helped as well. 

Alexander
Contributor
September 24, 2018

You did not understand me. The value of the assignee field is not copied to the custom field (technology).
I need to fix this.

Tarun Sapra
Contributor
September 24, 2018

Yes, & as I have already told you, please create an ApplicationUser object from the assignee.

Alexander
Contributor
September 24, 2018

Can you give an example script?

Tarun Sapra
Community Champion
September 25, 2018

Hello @Alexander

Along with the changes which I have shared already, just change your code from

 def assignee = issue.getAssigneeId()

to

 def assignee = issue.getAssignee() 

And it should work

0 votes
Rambabu Patina _Appfire_
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, 2018

@Alexander, Can you please provide us more details on what is the error you are getting, which add-on using and your JIRA version.

Alexander
Contributor
September 21, 2018

Jira v7.3.7
ScriptRunner Version: 5.4.12 

error log:
2018-09-21 12:14:26,347 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-09-21 12:14:26,347 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: PB-441, actionId: 331, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.getValueFromIssue(AbstractSingleFieldType.java:71) at com.atlassian.jira.issue.fields.ImmutableCustomField.getValue(ImmutableCustomField.java:350) at com.atlassian.jira.issue.fields.CustomField$getValue$3.call(Unknown Source) at Script921.run(Script921.groovy:15)

Suggest an answer

Log in or Sign up to answer