I have a custom scriptrunner listener that checks if the Components field was updated, and if it was retrieve the Component lead for the first component in the list. Then I want it to set the assignee to that component lead.
I know it is retrieving the component lead correctly, but the script is not setting the assignee as expected and I am not sure why.
I must be missing something, can someone help me know what I am doing wrong? Here is my code:
I can't see anything wrong with your update method. B
I'm not sure what's going on with your while loop, however. And maybe some part of the script is missing. I don't see when "user" is defined and where "findFieldsMentionedInChangeLog" is defined.
The whole thing can be simplified to:
/* get the changed fields above */
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
if (changedFields.contains("Component")) {
def componentLeads = issue.components*.componentLead
if (componentLeads) {
issue.assignee = componentLeads.first()
ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
The one thing I'll add is that you are missing code to re-index the issue. After this listener runs, you will be able to see the new assignee when you open the issue, but if you search for issues with that assignee, the issue will not come up because the issue will not have been added to search index.
Here is how I re-index an issue:
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
Thank you for your help.
The loop was doing what the simplified code you gave me was doing, so I have updated my code. I have also added the index code too and now everything is working.
Below is my updated code that works if anyone else is interested. Still not exactly sure why my code before wasn't working, other than maybe the first parameter to the updateIssue may have been incorrect.
________________________________________________
import com.atlassian.jira.component.ComponentAccessor;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Be careful, I fear that your current code might run with every event update whether the component is modified or not.
Unless there is a new feature in ScriptRunner in a version that I have not upgraded to yet, the following is NOT a built-in function and probably doesn't do anything:
def changedFields = findFieldsMentionedInChangeLog(changelog)
Also, you are not re-using this "changeFields" variable anywhere subsequently.
Here is a full script that should work:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
event = event as IssueEvent
// Checking if the "Components" field value has changed.
def changeHistoryManager = ComponentAccessor.changeHistoryManager;
def changeItems = changeHistoryManager.getChangeHistoryById(event.changeLog.id as Long).changeItemBeans
if(!changeItems.any{it.field == 'Components'}){
return //nothing to do when Components is not changed
}
MutableIssue issue = event.issue as MutableIssue
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
// Component field has changed, then set the Component Lead as the Assignee
if (changedFields.contains("Component")) {
def componentLeads = issue.components*.componentLead
if (componentLeads) {
issue.assignee = componentLeads.first()
ComponentAccessor.issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
// Performing reindex of issue
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
I am trying to use the last script you supplied, but on this line:
if(!changeItems.any{it.field == 'Components'}){
return //nothing to do when Components is not changed
}
It is always returning true and exiting, no matter what field is being updated. Any idea why?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try with
if(!changeItems.any{it.field == 'Component/s'}){
return //nothing to do when Components is not changed
}
Generally, the changeItem field is the same string that you'll see in your audit history tab for your issue. If you see something else inf your instance, try with that.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The Change History is using "Component/s" and I have tried that, but I get the same result. It never recognizes this field has been modified.
I finally figured out it is using just "Component", without the "s". I have done a bit of testing, and it is now working. Strange that it doesn't have the s on the field name though.
Thanks again for your help!
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.