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

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

Accepted Answer
3 votes

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.

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

 

 

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

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

 

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")
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

3,648 views 6 6
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you