Custom listener to detect User Picker field change and copy value to AssigneeU

WP Service Account May 25, 2016

I've coded the custom listener below to detect a change to the "Developer" field and update the Assignee field with the new "Developer" value. "Developer" is a custom user picker field

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.ofbiz.core.entity.GenericValue;
import org.ofbiz.core.entity.GenericEntityException;
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.user.ApplicationUser
log.setLevel(org.apache.log4j.Level.INFO)
log.info("start");
 
Issue issue = event.issue
 
// Check if Developer changed
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Developer"}
def userCf1 = customFieldManager.getCustomFieldObjectByName("Developer")
def DevUser = issue.getCustomFieldValue(userCf1) as ApplicationUser
//if Developer changed update the Assignee
if (change) {
    log.info ("Developer changed")
    issue.setAssigneeId(DevUser.username.toString())
} else {
    log.info ("Developer did not change")
}

 

My listener has the following error, and I cannot get it to set the Assignee field with the value from "Developer"

 

Error.PNG

 

Help greatly appreciated!

 

Regards,

Conor

 

PS - will a custom listener detect changes from bulk updates?

 

1 answer

1 accepted

3 votes
Answer accepted
Nic Brough -Adaptavist-
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 25, 2016

The Issue object doesn't have that method, you need to be using a MutableIssue (from memory, a simple cast will do it - MutableIssue issue = event.issue)

I also suspect you can just use setAssignee (DevUser) instead of extracting a string and then getting it to convert it back to an applicationuser.

WP Service Account May 25, 2016

Hi Nick,

Based on your answer I did the following. Was getting errors by doing exactly the above

1.

MutableIssue issue = event.issue as MutableIssue

2.

if (change) {
log.info ("Developer changed")
issue.setAssigneeId(DevUser.username.toString())
def dev_val = DevUser.username.toString()
log.info ("Set Assignee to $dev_val")
}

 

I don't get any errors, and the logs show the script identifying the change and setting Assignee.

However this is not reflected in the JIRA issue. Assignee has not been updated.

2016-05-26 09:41:40,328 http-bio-8080-exec-2 INFO mccooyc487 581x61518x1 17prd5h 10.160.19.115,10.168.45.192 /secure/QuickEditIssue.jspa [onresolve.scriptrunner.runner.ScriptRunnerImpl] start
2016-05-26 09:41:40,332 http-bio-8080-exec-2 INFO mccooyc487 581x61518x1 17prd5h 10.160.19.115,10.168.45.192 /secure/QuickEditIssue.jspa [onresolve.scriptrunner.runner.ScriptRunnerImpl] Developer changed
2016-05-26 09:41:40,332 http-bio-8080-exec-2 INFO mccooyc487 581x61518x1 17prd5h 10.160.19.115,10.168.45.192 /secure/QuickEditIssue.jspa [onresolve.scriptrunner.runner.ScriptRunnerImpl] Set Assignee to mccooyc487

 

 

Nic Brough -Adaptavist-
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 26, 2016

That looks right to me, but isn't there more code on the end of this to store the updated issue object?

WP Service Account May 26, 2016

Script now looks like below, but having problem with IssueManager,updateIssue

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.ofbiz.core.entity.GenericValue;
import org.ofbiz.core.entity.GenericEntityException;
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.ApplicationUser;
log.setLevel(org.apache.log4j.Level.INFO)
log.info("start");
 
MutableIssue issue = event.issue as MutableIssue
IssueManager issueManager = ComponentAccessor.getIssueManager();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getUser();

// Check if Developer changed
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Developer"}
def userCf1 = customFieldManager.getCustomFieldObjectByName("Developer")
def DevUser = issue.getCustomFieldValue(userCf1) as ApplicationUser
//if Developer changed update the Assignee
if (change) {
    log.info ("Developer changed")
    issue.setAssigneeId(DevUser.username.toString())
    def dev_val = DevUser.username.toString()
    log.info ("Set Assignee to $dev_val")
    issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_ASSIGNED, true)
} else {
    log.info ("Developer did not change")
}

 

Error -

Error2.PNG

 

WP Service Account May 26, 2016

Figured it out. Working script as below -

Thanks for your help Nick 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
log.setLevel(org.apache.log4j.Level.INFO)
log.info("start");
 
IssueManager issueManager = ComponentAccessor.getIssueManager();
def mutableIssue = issueManager.getIssueObject(event.issue.id)
// Check if Developer changed
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Developer"}
def userCf1 = customFieldManager.getCustomFieldObjectByName("Developer")
def DevUser = mutableIssue.getCustomFieldValue(userCf1) as ApplicationUser
def BAUser = customFieldManager.getCustomFieldObjectByName("BA / Designer")
def TestUser = customFieldManager.getCustomFieldObjectByName("Tester")
//if Developer changed update the Assignee
if (change) {
    log.info ("Developer changed")
    mutableIssue.setAssigneeId(DevUser.username.toString())
    mutableIssue.setCustomFieldValue(BAUser, null)
    mutableIssue.setCustomFieldValue(TestUser, null)
    def dev_val = DevUser.username.toString()
    log.info ("Set Assignee to $dev_val")
    issueManager.updateIssue(event.getUser(), mutableIssue, EventDispatchOption.ISSUE_ASSIGNED, false);
} else {
    log.info ("Developer did not change")
}
Paweł Ogrodnik July 5, 2019

Hi @WP Service Account I have similar problem.

I need update user picker field from assignee, but from other project

Like Ufuk Uysal likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events