Script to copy Assignee field to other userpicker custom field doesn't update the field.

Aravindi Amarasinghe April 17, 2019

Hi All, 

Good day! 

I have a script written to update "Temp Assignee" custom field. In logs, I can see Temp Assignee picks the user name, but cannot see Temp Assignee field get updating in the view screen. 

Can someone help me to find the issue here? 

I am using JIRA v 6.4.3, Script Runner 3.0.12

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.CustomFieldManager;

def projectComponentManager = ComponentAccessor.getProjectComponentManager()


//Script console Test
//def issue = ComponentLocator.getComponent(IssueManager).getIssueObject("KWB-1546");

//def project = issue.getProjectObject()

def customFieldManager = ComponentAccessor.getCustomFieldManager()


// a user custom field
def userCf = customFieldManager.getCustomFieldObjectByName("Temp Assignee")
def assignee = issue.getAssignee()
issue.setCustomFieldValue(userCf, assignee)

def TAValue = issue.getCustomFieldValue(userCf)
log.error("Temp Assignee : " + TAValue)



Logs are as follows, 

2019-04-18 14:00:08,694 http-bio-8081-exec-21 ERROR e5433464 840x12689x1 14t5y25 10.56.24.10 /secure/WorkflowUIDispatcher.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Temp Assignee : e10111111:10000

 

2 answers

2 accepted

1 vote
Answer accepted
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2019

Hello @Aravindi Amarasinghe 

You have used

issue.setCustomFieldValue(userCf, assignee)

This doesn't update the issue, as per the docs

Sets a custom field value on this Issue Object, but does not write it to the database. This is highly misleading. 

Hence you should either use the issueManager as described in the answer by @Elifcan Cakmak or use the following snippet to update custom field

userCf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(userCf), assignee), new DefaultIssueChangeHolder())

Also, please go through the following link

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

0 votes
Answer accepted
Elifcan Cakmak
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.
April 18, 2019

Hello,

After setting the customfield value you need to update issue as well:

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser

def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def user = authenticationContext.getLoggedInUser()

// your code here

// This is what you are missing:
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, 
false)

 Regards.

Aravindi Amarasinghe April 28, 2019

Hi @Elifcan Cakmak 

Thanks for posting an answer I modified my script as you instructed. But no luck! :(

It picks up the user but not updating the field

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.event.type.EventTypeManager;


def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def user = authenticationContext.getLoggedInUser()
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
ComponentManager componentManager = ComponentManager.getInstance()
IssueManager issueManager = componentManager.getIssueManager()

//Script console Test
//def issue = ComponentLocator.getComponent(IssueManager).getIssueObject("AJT-86");

//def project = issue.getProjectObject()

def customFieldManager = ComponentAccessor.getCustomFieldManager()


// a user custom field
def userCf = customFieldManager.getCustomFieldObjectByName("Temp Assignee")
def assignee = issue.getAssignee()
issue.setCustomFieldValue(userCf, assignee)
//userCf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(userCf), assignee), new DefaultIssueChangeHolder())

def TAValue = issue.getCustomFieldValue(userCf)
log.error("Temp Assignee : " + TAValue)

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

And I get these logs. 

at Script84.run(Script84.groovy:37)
at com.atlassian.jira.issue.IssueManager$updateIssue$0.call(Unknown Source)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:860)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:875)
at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:913)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:504)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:522)
at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(CustomFieldImpl.java:854)
at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:161)
at com.atlassian.jira.issue.customfields.impl.UserCFType.getDbValueFromObject(UserCFType.java:80)
java.lang.ClassCastException: com.atlassian.jira.user.BridgedDirectoryUser cannot be cast to com.atlassian.jira.user.ApplicationUser
2019-04-29 15:11:08,525 http-bio-8081-exec-26 ERROR e5400000 911x21440x1 1xcyo27 10.56.24.10 /secure/WorkflowUIDispatcher.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: AJT-87, actionId: 1091, file: <inline script>
2019-04-29 15:11:08,525 http-bio-8081-exec-26 ERROR e5400000 911x21440x1 1xcyo27 10.56.24.10 /secure/WorkflowUIDispatcher.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] *************************************************************************************
2019-04-29 15:11:08,524 http-bio-8081-exec-26 ERROR e5400000 911x21440x1 1xcyo27 10.56.24.10 /secure/WorkflowUIDispatcher.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Temp Assignee : e111111111:10000

Elifcan Cakmak
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.
April 28, 2019

Hi,

Issue is not updating because of this error: java.lang.ClassCastException: com.atlassian.jira.user.BridgedDirectoryUser cannot be cast to com.atlassian.jira.user.ApplicationUser

This means that you are trying to set a bridged directory user to user picker custom field because user picker custom fields wants an application user instead. This happens when you use crowd for user auth on Jira.

Here you should debug the code:

def assignee = issue.getAssignee()
issue.setCustomFieldValue(userCf, assignee)

 You can get class of assignee with assignee.getClass(). It will return a bridged directory user. You should convert this to application user.

Regards. 

Aravindi Amarasinghe May 15, 2019

Hi @Elifcan Cakmak 

Thanks for posting answers. I tried replacing the above by 

issue.setCustomFieldValue(userCf, assignee.getClass())

I get the same error  

java.lang.ClassCastException: java.lang.Class cannot be cast to com.atlassian.jira.user.ApplicationUser
2019-05-15 12:00:12,160 http-bio-8081-exec-46 ERROR e5433464 720x3854x1 1yl0zlz 10.56.24.10 /secure/CommentAssignIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: KWB-1547, actionId: 331, file: <inline script>

Elifcan Cakmak
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.
May 16, 2019

Hello,

This is because assignee.getClass() returns the Class of the object. Such as string, int, application user etc. I wrote:

 You can get class of assignee with assignee.getClass(). It will return a bridged directory user. You should convert this to application user.

As I said the error is is that you cannot cast anything else besides application user to application user. That's why you should somehow convert bridged directory user to application user.

Regards.

Like Tarun Sapra likes this
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 16, 2019

Hello @Aravindi Amarasinghe 

I suggest you to also follow some basic java tutorials as errors like

"java.lang.ClassCastException: java.lang.Class cannot be cast to com.atlassian.jira.user.ApplicationUser"

Are pretty straightforward to resolve, but if you are struggling with these errors I suggest you to have some tutorials otherwise you will only endup struggling more with more of such errors.

Suggest an answer

Log in or Sign up to answer