Forums

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

Set assignee from component lead using script listener

Alvin
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 Champions.
September 13, 2018

Hello Community, I need help on scriptrunner, I need to set assignee based on the component's lead. Below is the code that I'm using

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption


// a map with select list option to Component
def componentsMap = [
"BOSS" : "Component1",
"Mashery" : "Component2",
]

def issue = event.issue as MutableIssue

def selectList = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Affected Service")
def selectListValue = issue.getCustomFieldValue(selectList) as LazyLoadedOption

// there is no value for the select list - therefore do nothing
if (! selectListValue) {
return
}

def componentManager = ComponentAccessor.projectComponentManager
def componentName = componentsMap.get(selectListValue.value)
def component = componentManager.findByComponentName(issue.projectObject.id, componentName)
def userManager = ComponentAccessor.userManager
def components = issue.componentObjects.toList()

if (component) {
componentManager.updateIssueProjectComponents(issue, [component])
}
issue.setAssignee(ComponentAccessor.userManager.getUserByKey(component.lead))

 

In this code, I have Affected Service as a single select list, if BOSS has been selected, the Component Field will update and set to Component1, but the assignee remains unassigned even if I have default assignee on my component. I know that there's something wrong with my code because I'm a beginner. I need to change assignee everytime component field is changed, that's why I used script listener. Thanks

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Antoine Berry
Community Champion
January 26, 2022

Hello @Drishti Maharaj ,

I just tried your script. It is working fine, I only had to add this line at the end to update the issue : 

ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.getUserManager().getUserByKey('user_key'), issue, EventDispatchOption.DO_NOT_DISPATCH, false);

Of course you will need to map all the priorities in your switch but that does not seem to be the issue.

Let me know if that helped.

Drishti Maharaj
Contributor
January 26, 2022

Hi @Antoine Berry , thank you for taking the time to reply, if I add the additional script from you, the following error occurs:

Screenshot 2022-01-27 082910.png

Drishti Maharaj
Contributor
January 26, 2022

Hi @Antoine Berry , I managed to figure out what was wrong :)

Just needed this at the top:

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

and it worked perfectly! 

Thank you so much!

Like Antoine Berry likes this
TAGS
AUG Leaders

Atlassian Community Events