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
Ramakrishnan Srinivasan
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.
March 13, 2019

The following code works; observe

  • first of all, it is addCustomFieldValue
  • that project picker id has to be converted to string
  • user has to be username
  • customfields passed should be with their ids
//https://community.atlassian.com/t5/Marketplace-Apps-questions/ScriptRunner-automation-rules-add-a-value-to-a-custom-field-list/qaq-p/868166
//https://community.atlassian.com/t5/Jira-questions/script-of-add-values-to-single-select-list-custom-field/qaq-p/872349
//https://community.atlassian.com/t5/Jira-questions/script-of-add-values-to-single-select-list-custom-field/qaq-p/872349


//https://community.atlassian.com/t5/Jira-questions/Create-Issue-in-project-depending-on-custom-field-ScriptRunner/qaq-p/58630

// IMPORTANT LINK DO NOT DELETE https://community.atlassian.com/t5/Marketplace-Apps-questions/Create-issue-with-IssueService/qaq-p/706336
import org.apache.log4j.Level
import org.apache.log4j.Logger
def log = Logger.getLogger("com.onresolve.jira.groovy")
log.setLevel(Level.DEBUG)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager

def authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
def customFieldManager =ComponentAccessor.getCustomFieldManager()
def projectMgr = ComponentAccessor.getProjectManager()
def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//reference issue
def issue = issueService.getIssue(user, "POWN-1").getIssue()
def IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();
//log.info(user.id)

def projectManager = ComponentAccessor.getProjectManager()
def projects = projectManager.getProjectObjects()
def productToOwn = projectManager.getProjectObjByKey("AGEIR")
//log.info(productToOwn.id)
def pkey = productToOwn.key.replaceAll("Project: ", "")
//log.info(pkey)
def PTOCustomField = customFieldManager.getCustomFieldObjectByName("Product to Own")
//log.info(PTOCustomField)


CustomFieldManager customFieldManager_EU = ComponentAccessor.getCustomFieldManager()
CustomField cf_EU = customFieldManager_EU.getCustomFieldObjectByName("Executive Owner")
def userManager_EU = ComponentAccessor.getUserManager()
// Use this user to set the user picker to
def user_EU = userManager_EU.getUserByName("srinivasanr")
//log.info(user_EU.username)

//https://community.atlassian.com/t5/Marketplace-Apps-questions/Post-script-function-multiple-user-picker-custom-field-update/qaq-p/352376

log.debug ("Initialize inputparameters")
issueInputParameters.setProjectId(projectMgr.getProjectObjByKey("POWN").id)
issueInputParameters.setIssueTypeId(issue.issueTypeId)
issueInputParameters.setSummary("AGEIR Ownership")

issueInputParameters.addCustomFieldValue(PTOCustomField.id, productToOwn.id.toString())
//issueInputParameters.skipScreenCheck()
issueInputParameters.addCustomFieldValue(cf_EU.id,user_EU.username)


IssueService.CreateValidationResult validationResult = issueService.validateCreate(authenticationContext.getLoggedInUser(), issueInputParameters);
//issueObject.getReporter()
def newIssue

if(!validationResult.isValid()) {
log.debug("Could not create issue: " + validationResult.getErrorCollection());
} else {
// Create new issue
log.debug ("Creating new issue")
def createResult = issueService.create(authenticationContext.getLoggedInUser(), validationResult)
newIssue = createResult.getIssue()
log.info(newIssue)

if (!createResult.isValid()) {
log.debug("Issue was not created!")
} else {
log.debug("Issue was created. Issue Id: " + createResult.getIssue().getKey())
}
}
Ramakrishnan Srinivasan
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.
March 13, 2019

logging to the server is working but to the console after the create is still not working for me

TAGS
AUG Leaders

Atlassian Community Events