I have a script listener that assigns an issue (and moves it into 'To Do') when a different issue is resolved. This script works fine. However, I'm getting a deprecation comment when calling MutableIssue.store().
I want to know how to properly use the more up-to-date solution as per the deprecation comment: 'Use the Object's Service or Manager to save values. Since v5.0'. The reason I want to update the code is twofold:
The important portion of the code I'm currently using is as follows:
ApplicationUser assignee = ComponentAccessor.getUserManager().getUserByName(newAssignee)
remedIssue.setAssignee(assignee)
remedIssue.store()
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class)
workflowTransitionUtil.setIssue(remedIssue);
workflowTransitionUtil.setAction(101);
workflowTransitionUtil.progress();
It should be noted that I'm getting the deprecation notice on the line
remedIssue.store()
The modified version of the code attempting to use the modern approach with the hope it will send the appropriate 'On Assigned' email notification:
ApplicationUser assignee = ComponentAccessor.getUserManager().getUserByName(newAssignee)
remedIssue.setAssignee(assignee)
ApplicationUser assigningUser = ComponentAccessor.getUserManager().getUserByName("admin")
IssueManager issueManager = ComponentAccessor.getIssueManager()
issueManager.updateIssue(assigningUser, remedIssue, EventDispatchOption.ISSUE_ASSIGNED, true)
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class)
workflowTransitionUtil.setIssue(remedIssue);
workflowTransitionUtil.setAction(101);
workflowTransitionUtil.progress();
However, when I run this new script, I get a huge stack trace in the log, and the following error when trying to perform the action which triggers this script (resolve a subtask):
org.ofbiz.core.entity.GenericTransactionException: Commit failed, rollback previously requested by nested transaction.
I'm using Jira version 7.9.2. Here is the complete script for purposes of completeness:
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
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.mail.Email;
import com.atlassian.mail.server.MailServerManager;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.issue.JiraIssueEvent
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.event.type.EventDispatchOption
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
def coordinatorUserName = "someUser"
def leadUserName = "someOtherUser"
def newAssignee = ""
log.info event.toString()
if(issueType == "SomeIssueType" || issueType == "SomeOtherIssueType"){
if (event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Review Completion Status"}) {
def newValue = event.issue.getCustomFieldValue(reviewCompletionStatusField)
MutableIssue remedIssue = null
Issue parent = event.issue.getParentObject()
def subtaskToAssignName = "";
switch(newValue){
case "Not Approved":
newAssignee = coordinatorUserName
subtaskToAssignName = "SomeSubtaskName"
break;
case "Approved":
case "Approved With comments":
newAssignee = leadUserName
subtaskToAssignName = "SomeOtherSubtask"
break;
}
parent.getSubTaskObjects().each{sibling->
if(sibling.getIssueType().name == subtaskToAssignName){
remedIssue = sibling as MutableIssue
}
}
if(remedIssue != null){
ApplicationUser assignee = ComponentAccessor.getUserManager().getUserByName(newAssignee)
remedIssue.setAssignee(assignee)
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser assigningUser = ComponentAccessor.getUserManager().getUserByName("admin")
issueManager.updateIssue(assigningUser, remedIssue, EventDispatchOption.ISSUE_ASSIGNED, true)
WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class)
workflowTransitionUtil.setIssue(remedIssue);
workflowTransitionUtil.setAction(101);
workflowTransitionUtil.progress();
}
}
}
Hello @Jake Jollimore
As you may notice
JiraUtils.loadComponent
is deprecated too. I would recommend you to use IssueService instead of workflowUtils
Like this
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.Issue
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.DEBUG)
def reviewCompletionStatusField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10209")
def epicLinkField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10100")
def issueType = event.issue.issueType.name
def coordinatorUserName = "someUser"
def leadUserName = "someOtherUser"
def newAssignee = ""
log.info event.toString()
if(issueType == "SomeIssueType" || issueType == "SomeOtherIssueType"){
if (event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Review Completion Status"}) {
def newValue = event.issue.getCustomFieldValue(reviewCompletionStatusField)
MutableIssue remedIssue = null
Issue parent = event.issue.getParentObject()
def subtaskToAssignName = "";
switch(newValue){
case "Not Approved":
newAssignee = coordinatorUserName
subtaskToAssignName = "SomeSubtaskName"
break;
case "Approved":
case "Approved With comments":
newAssignee = leadUserName
subtaskToAssignName = "SomeOtherSubtask"
break;
}
parent.getSubTaskObjects().each{sibling->
if(sibling.getIssueType().name == subtaskToAssignName){
remedIssue = sibling as MutableIssue
}
}
if(remedIssue != null){
ApplicationUser assignee = ComponentAccessor.getUserManager().getUserByName(newAssignee)
remedIssue.setAssignee(assignee)
IssueManager issueManager = ComponentAccessor.getIssueManager()
IssueService issueService = ComponentAccessor.getIssueService()
ApplicationUser assigningUser = ComponentAccessor.getUserManager().getUserByName("admin")
issueManager.updateIssue(assigningUser, remedIssue, EventDispatchOption.ISSUE_ASSIGNED, true)
def issueInputParameters = issueService.newIssueInputParameters()
def validationResult = issueService.validateTransition(assigningUser, remedIssue.id, 101, issueInputParameters)
if (validationResult.isValid()) {
log.debug("Validation Result is valid")
// Perform the transition
def issueResult = issueService.transition(assigningUser, validationResult)
if (!issueResult.isValid()) {
log.debug("Failed to transition task ${remedIssue.key}, errors: ${issueResult.errorCollection} ")
}
} else {
log.debug("Could not transition task ${remedIssue.key}, errors: ${validationResult.errorCollection}")
}
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.