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

How retrieve a value base when value selected from DB picker field?

Shah Baloch January 24, 2022

Hi,

I created a DB picker field "Location Name" which displays all locations from a drop-down field. That is working fine, we are using it for multiple projects.

For a new project, we would like to do some automation. We would like to retrieve a person's name to a text field if a location is selected from the DB picker field. We have accounts from different locations and multiple locations are assigned to a person. for example, we have 5 Reps and each rep are assigned 10 locations. So when the user is creating an issue and selecting a location from the DB picker drop down it should add the person to the text field (Representative) that belongs to that location.

The below listener script works fine with Jira custom fields but it is not working with the DB picker field. I mean instead of DB picker if I use a text field and type that location name that exists in the database it'll pull up the rep name, but for some reason, it's not working with the ScriptRunner DB picker field.

Any idea how I can make it works?

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.db.DatabaseUtil
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def event = event as IssueEvent
def issue = event.issue
def customFieldObjects = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)

def locationName = customFieldObjects.findByName("Location Name")
def rep = customFieldObjects.findByName("Representative")

def value = issue.getCustomFieldValue(locationName)

DatabaseUtil.withSql('Mysql_Accounts_DB') { sql ->
def row = sql.firstRow("""SELECT customer_id, LocationName, AssignedRepPerson, AssignedRep_id
FROM Accounts WHERE LocationName = ?""", value)
if (row) {

rep.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(rep), row.'AssignedRepPerson'), new DefaultIssueChangeHolder())
}
}

Thank you

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events