Set assignee based on custom field value

Jimit Jhaveri July 19, 2018

I am having an issue where I am trying to set an assignee based on a custom field value. This current code block below shows no errors when editing and I also get no errors when running in a workflow post function. However, the assignee of the field does not change. What am I doing wrong?

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.issue.MutableIssue

def issueManager = ComponentAccessor.getIssueManager();
def userManager = ComponentAccessor.getUserManager();
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def cField = customFieldManager.getCustomFieldObject("customfield_12400");
def cFieldValue = issue.getCustomFieldValue(cField);

if (cFieldValue == "Security") {
def assignee = userManager.getUserByKey("i849194");
issue.setAssignee(assignee);
}
else if (cFieldValue == "Operations") {
def assignee = userManager.getUserByKey("i826121");
issue.setAssignee(assignee);
}

else if (cFieldValue == "Instance Creations/Backups") {
def assignee = userManager.getUserByKey("i825046");
issue.setAssignee(assignee);
}

 

3 answers

1 accepted

2 votes
Answer accepted
Alexey Matveev
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.
July 19, 2018

Hello,

Try to write it like this:

if (cFieldValue.toString() == "Security") {
def assignee = userManager.getUserByKey("i849194");
issue.setAssignee(assignee);
}
else if (cFieldValue.toString() == "Operations") {
def assignee = userManager.getUserByKey("i826121");
issue.setAssignee(assignee);
}

else if (cFieldValue.toString() == "Instance Creations/Backups") {
def assignee = userManager.getUserByKey("i825046");
issue.setAssignee(assignee);
}
Jimit Jhaveri July 19, 2018

Alexey, this worked! Thank you for your prompt response!

Like Alibek Malikov likes this
Alexey Matveev
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.
July 19, 2018

You are welcome!

Like Alibek Malikov likes this
1 vote
Cristian Rosas [Tecnofor]
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.
July 19, 2018

Hi Jimit,

Where is this post function? In create transition or another? Also, in which order is it?

Jimit Jhaveri July 19, 2018

It is the first transition after create. So the second transition. Currently this post function is at the top of the list in terms of order to follow. 

0 votes
Jake Jollimore July 19, 2018

I'm having a similar issue. I was going to open my own question, but I think the answer you get will be the same as the answer I need, so maybe I can contribute a bit. 

I think the problem has to do with needing to persist the data. Here's the code I'm trying to get to work:

 

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue

log.setLevel(org.apache.log4j.Level.DEBUG)

def reviewCompletionStatusField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10209")
def epicLinkField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10100")
def issueType = event.issue.issueType.name

//Initial/Validation Review:
//if Not Approved, assign triage subtask to coordinator and put it into todo
//if Approved with comments or approved, assign Canada Initial Review subtask to canada contact (tbd) and put it into todo, also notify coordinator

//Approved, Approved With Comments, Not Approved

log.info event.toString()

//Only do work if the task that changed was DND Initial Review or DND Validation Review
if(issueType == "ADS Initial Review" || issueType == "ADS Validation Review"){
log.info "Triggered by change made to issue ${event.issue}"

if (event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Review Completion Status"}) {

def newValue = event.issue.getCustomFieldValue(reviewCompletionStatusField)
log.info "The Review Completion Status field has changed to ${newValue}"

MutableIssue remedIssue = null
Issue parent = event.issue.getParentObject()
def subtaskToAssignName = "";

switch(newValue){
case "Not Approved":
subtaskToAssignName = "Triage"
break;
case "Approved":
case "Approved With comments":
subtaskToAssignName = "Initial Review"
break;
}


parent.getSubTaskObjects().each{sibling->
if(sibling.getIssueType().name == subtaskToAssignName){
remedIssue = sibling as MutableIssue
log.info "Remediation issue found: ${remedIssue.key} - ${remedIssue.summary}"
}
}

if(remedIssue != null){
// Issue epic = parent.getCustomFieldValue(epicLinkField) as Issue
ApplicationUser coordinator = ComponentAccessor.getUserManager().getUserByName("someUserName")
log.info "The coordinator lead for this task is ${coordinator}"
remedIssue.setAssignee(coordinator)

ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

if (!loggedInUser) {
loggedInUser = UserManager.getUserByName('jira_bot')
}

def validateAssignResult = ComponentAccessor.getIssueService().validateAssign(loggedInUser, issue.id, coordinator.getKey())
ComponentAccessor.getIssueService().assign(loggedInUser, validateAssignResult)
}
}
}

I think the line that's giving me some trouble is towards the end:

def validateAssignResult = ComponentAccessor.getIssueService().validateAssign(loggedInUser, issue.id, coordinator.getKey())

I think I need to find a way to get the user id in string format to replace 'coordinator.getKey()'

Suggest an answer

Log in or Sign up to answer