Hello,
I need a listener that assigns the issue to a default assignee, whenever the issues workflow is transitioned to a specific status. My idea:
1. Listener listens to updates of the issues --> then fires
2. If worflow status matches the corresponding ID and issueType matches corresponding issuetype --> the assigne should be set to XY
my code here:
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserManager
//Workflow imports
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
// Variabels
String issueTypeId_IST = "10202"
def WfId_Soll = "2"
//Workflow
WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager()
//Preps
def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager()
ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def user = userManager.getUserByName("lehrkraftsuchen")
log.info("step1: Listener fires due to updating issues")
// Get Issue
def issue = event.getIssue()
JiraWorkflow workflow = workflowManager.getWorkflow(issue)
def issueTypeId_IST = issue.getIssueTypeId()
def WfId_IST = workflow.getLinkedStep(issue.getStatus()).id
log.info("das Issue lautet:"+issue)
log.info("Issuetype ID IST:"+issueTypeId_IST)
log.info("Issuetype ID SOLL:"+issueTypeId_Soll)
log.info("WF ID IST:"+ WfIdI_ST)
log.info("WF ID SOLL:"+ WfId_Soll)
if (issueTypeId_IST != issueTypeId_Soll || WfId_IST != WfId_Soll) {
log.info("IF clause does not match")
return
}
def validateAssignResult = issueService.validateAssign(loggedInUser, issue.id, "lehrkraftsuchen")
issueService.assign(loggedInUser, validateAssignResult)
log.info("der assignee wurde geändert"+ issue.getAssignee())
As you see I use log.info with some interestin resoults. In the logs I can see that everything matches the way I want it to match and still the code gets returned, due to the if clause. but this is not logical. Neither the issuetype or the WFId ist "unequalt" a true value...
Why does this code doesnt work? I also tried this if-clause:
if (issueTypeId_IST == issueTypeId_Soll && WfId_IST == WfId_Soll){
**further code**
}
Thanks for your aswers
I can't see where issueTypeId_Soll is declared and set.
And I see you first set issueTypeId_IST to 10202 and then later to the issueTypeId.
That might be why your if block is not working as you expect.
But let me ask a more fundamental question ... if this is only expected to fire on a single workflow when the status changes. Why don't you implement this as a postfunction IN the workflow instead?
If you need this to work accrosss multiple workflows, then listener is appropriate. But I generally use the event.changeLog to detect a change
Here is a sample using some of your script variables and elements
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser
def issueService = ComponentAccessor.issueService
def changeHistoryManager = ComponentAccessor.changeHistoryManager
def constantsManager = ComponentAccessor.constantsManager
def WfId_Soll = '2'
def issueTypeId_Soll = '10202'
def defaultAssigneeName = 'lehrkraftsuchen'
def issue = event.issue as Issue
if(issue.issueTypeId != issueTypeId_Soll){
//wrong issue type nothing to do
return
}
if (!event.changeLog){
//no change log, so status was definitely not changed
return
}
//get the list of changes associated with the event
def changeItems = changeHistoryManager.getChangeHistoryById(event.changeLog.id as Long).changeItemBeans
//Check if the status was changed
if(!changeItems.any{ it.field.equalsIgnoreCase('status')}){
//the status was not changed during this event
return
}
def statusChangeItem = changeItems.find { it.field.equalsIgnoreCase('status') }
//get the status object for the status after the change
def afterStatus = constantsManager.getStatusByNameIgnoreCase(statusChangeItem.getToString())
if(afterStatus.id != WfId_Soll ){
//the new status is not one I care about
return
}
ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def validateAssignResult = issueService.validateAssign(loggedInUser, issue.id, defaultAssigneeName)
if(validateAssignResult.isValid()) {
issueService.assign(loggedInUser, validateAssignResult)
} else {
log.error validateAssignResult.errorCollection
}
Earning the Mindful Member badge proves you know how to lead with kindness, plus it enters you into a giveaway for exclusive Atlassian swag. Take the quiz, grab the badge, and comment on our announcement article to spread the good vibes!
Start here
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.