Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner: WF transition to change Assignee

dD
Contributor
January 25, 2022

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

1 answer

1 vote
PD Sheehan
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.
January 29, 2022

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
}

Suggest an answer

Log in or Sign up to answer