Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

To set a single select using customlistener

Muddassir Quazi October 30, 2021

Hi,

 

We have a JSM datacenter v4.150 where script runner v6.37  is installed.

We get a single grouppicker from Groupee Group plugin which I have to use to set single select field 'parent'. 

For the same I have written a custom listener for issue created and issue update events.

The listener is :

import groovy.sql.Sql
import java.sql.Driver
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.customfields.view.CustomFieldParams
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.crowd.embedded.api.Group

def driver = Class.forName('org.postgresql.Driver').newInstance() as Driver

def props = new Properties()
props.setProperty("user", "username")
props.setProperty("password", "password")

def conn = driver.connect("jdbc URL", props)
def sql = new Sql(conn)

//sql.rows("SELECT table_name FROM information_schema.tables WHERE table_schema='public' AND table_type='BASE TABLE'")
def issueManager = ComponentAccessor.getIssueManager()
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
//def issue = issueManager.getIssueObject("INC-18")
def issue = event.issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueService = ComponentAccessor.issueService
def groupManager = ComponentAccessor.getGroupManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def site = customFieldManager.getCustomFieldObject("customfield_10902") //single group picker
//def catSubCat = customFieldManager.getCustomFieldObject("customfield_10711")
def parent = customFieldManager.getCustomFieldObject("customfield_11908") //single select
def siteValue = issue.getCustomFieldValue(site).name[0]
log.error"yes sitevalue= "+siteValue
//def selectedSiteValue = issue.getCustomFieldValue(siteValue) as Map
//log.error"value is= "+selectedSiteValue
//def selectedSiteValue = siteValue.get()
//def selectedChildValue = catSubCatValue.get("1")
//def issuetype = issue.getIssueType()
//log.error"*******"+issuetype.isSubTask()+"--"+issue.getKey()
//log.error"*******"+issuetype.isAgile Change()+"--"+issue.getKey()
//if(!(issuetype.isSubTask()||issuetype.name=="Agile Change")){
// log.error"inside first if"

//if(!(issuetype.isAgile Change())){
// log.error"inside second if"
if(siteValue){
log.error"inside second if"
def query = "select * from parent_usergroup where usergroups='"+siteValue+"'"
log.error"no site value= "+query

def rows = sql.rows(query)
log.error"rows= "+rows
def assignmentGroup = null

if (rows.size() == 1 && rows[0].parent){
assignmentGroup = rows[0].parent
log.error"assignmentGroup= "+assignmentGroup
//return rows[0].assignment_group
}/*else if (rows.size() > 1){
def newquery = "select * from groupassignee where u_primary_affected_site='${siteValue}' and category='${selectedParentValue}' and subcategory='${selectedChildValue}'"
def rows1 = sql.rows(newquery)
if (rows1.size() > 0 && rows1[0].assignment_group){
assignmentGroup = rows1[0].assignment_group
//return rows[0].assignment_group
}
}*/

if(assignmentGroup){
// def cfConfig = parent.getRelevantConfig(issue)
//def act_value = optionsManager.getOptions(cfConfig)?.find { it.toString() == assignmentGroup }
log.error"inside if"

def issueInputParameters = issueService.newIssueInputParameters().with {
// set group fields

addCustomFieldValue(parent.id, assignmentGroup.toString())
}
log.error"issueInputParameters= "+issueInputParameters

def updateValidationResult = issueService.validateUpdate(loggedInUser, issue.id, issueInputParameters)
assert updateValidationResult.isValid(): updateValidationResult.errorCollection
log.error"updateValidationResult= "+updateValidationResult
def issueUpdateResult = issueService.update(loggedInUser, updateValidationResult, EventDispatchOption.ISSUE_UPDATED, false)
assert issueUpdateResult.isValid(): issueUpdateResult.errorCollection
log.error"issueUpdateResult= "+issueUpdateResult
}

return assignmentGroup
}
//}
//}
//}

 

But throwing error this time like

image.png

 

1 answer

0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 2, 2021

I'm not sure what you looking up with SQL but if you have to query a DB, I'd recommend setting up your DB in the Resources tab of Scriptrunner then using the DatabaseUtil.withSql() method instead of coding your own access to SQL.

But you are indicating that you want to apply a value to a Single Select based on the information you queried via SQL from the selected group.

It appears you are able to get the Group, then query the DB for the parent group.
This give you a group name.

But a Single Select field requires an Option object. I see you already have some commented-out code to get the act_value from the optionsManager. That was the correct direction.

I would do it like this:

if(assignmentGroup){
def cfConfig = parent.getRelevantConfig(issue)
def allOptions = optionsManager.getOptions(cfConfig)
def assignmentGroupOption = allOptions.find{ it.value == assignmentGroup}
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.addCustomFieldValue(parent.id, assignmentGroupOption.optionId as String)
}

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events