Hi everyone, hope all is well on your end.
With ScriptRunner listener, i am trying to get the Assignee updated whenever the ticket status gets updated. I see that in the History section, the Assignee did get updated, but it is not showing on the top left corner (UI) on the screen.
But when I update a field which i have tied to list of assignees based on the field values, the Assignee did got updated (both in History section and on the UI)
For the event mapping, i have set
1. For issue created, i use ISSUE_CREATED_ID
2. For issue updated, i use ISSUE_UPDATED_ID
3. For status changed, i use ISSUE_GENERICEVENT_ID
Here is my code for updating the assignee:
if(event && event.eventTypeId == ISSUE_GENERICEVENT_ID){
log.debug("Status Updated!!")
Boolean isStatusChanged = isStatusChangedEvent(event, issue)
if(isStatusChanged){
log.debug("isStatusChanged=$isStatusChanged")
updateAssignee(issue)
}
}
Boolean isStatusChangedEvent(IssueEvent event, MutableIssue issue){
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "status"}
if (change){
log.debug("isStatusChangedEvent")
return true
}else{
log.debug("isStatusChangedEventfalse")
return false
}
}
// Only fires when the eventTypeId is Generic Event
def updateAssignee(MutableIssue issue){
String cf_field_object = getFieldObject(issue)
String cf_field_value = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(cf_field_object))
String issueProject = issue.getProjectObject().key
String issueType = issue.issueType.name
String issueStatusRaw = issue.getStatus().name
String issueStatus = issueStatusRaw.toUpperCase()
String userID = ""
if (listProjects.contains(issueProject)){
if (issueType == "A") {
userID = "assigneeA"
} else if (issueType == "B"){
if (issueStatus == "statusB1"){
userID = "assigneeB1"
} else if (issueStatus == "statusB2"){
userID = "assigneeB2"
} else if (issueStatus == "statusB3"){
userID = "assigneeB3"
} else {
log.debug("Still same assignee")
return
}
}
}
ApplicationUser user = userManager.getUserByName(userID)
String userKey = user?.getName()
issueInputParameters.setAssigneeId(userKey)
}
Any pointers are greatly appreciated!
This suggests a problem with indexing, and it's probably to do with the very last line of code, as that is intended to be used when people have typed/selected something in an edit screen.
Try issue.setAssigneeId(userKey)
Yes, I think so too, oddly enough after I posted this question, saw from another thread suggesting to use
issue.setAssigneeId
instead of
issueInputParameters.setAssigneeId
https://community.atlassian.com/t5/Jira-questions/Change-assignee-when-issue-moves-Script-runner/qaq-p/1018536
Appreciate the pointer @Nic Brough -Adaptavist- !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
These two lines are odd
String userKey = user?.getName()
issueInputParameters.setAssigneeId(userKey)
setAssigneeId expects userkey - the variable name is right, but user.getName() returns username, not the key, I think the correct method is user.getKey()
The issueInputParameters itself seems in the wrong place, I suspect you're not including the whole method, or are defining it elsewhere?
Normally if you have a MutableIssue you could just do mutableIssue#setAssigneeId(String userkey), but this only changes the value in memory - you need to then update the issue in Jira via IssueManager, e.g.:
/*
event.issue should be "Issue" aka ImmutableIssue
this will return the MutableIssue object you can use to setAssigneeId on
MutableIssue mutableIssue = issueManager.getIssueObject(issue.getId())
*/
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
//Assuming "user" variable is your ApplicationUser
String userKey = user?.getKey() // will be e.g. "JIRAUSER012345"
if (userKey) {
IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue mutableIssue = issueManager.getIssueObject(event.issue.getId())
mutableIssue.setAssigneeId(userKey)
//user = who is doing the change, who will be shown in issue history as author of the update
//mutableIssue
//EventDispatchOption.ISSUE_UPDATED = event type to dispatch for this update
//false = whether to send email notifications for that event or not
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
}
However as you do refer to issueInputParameters I suspect there is other code in there somewhere. Both IssueInputParameters and MutableIssue are valid ways to update an issue, I just don't see the rest of the code and MutableIssue is simpler to use.
edit: added imports to the snippet, but you can use 'def' anywhere there and it'll work, just trying to show the difference between MutableIssue and Issue which is sometimes counter intuitive
edit2: sorry I felt off about this afterward and had to check in IDE, indeed I had a fault in the updateIssue method, now should be correct!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Appreciate the response, yes I did declare, just that forgot to include in the question.
Will try your suggestion. Thank you!
Long eventTypeId = event.eventTypeId
MutableIssue issue = (MutableIssue) event.issue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.