Hi,
In Jira data center v9.12.19, the "Related Projects" custom field type Database Picker.
The "Related ProjectsList" custom field type Select List(multiple choices).
>Main Goal: Copy value(s) from one field to another. The "Related Projects List" field is available to do Search for issues by JQL.
>The behaviour bellow, works with success:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.onresolve.scriptrunner.db.DatabaseUtil
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import com.onresolve.scriptrunner.db.DatabaseUtil
@BaseScript FieldBehaviours fieldBehaviours
// --- LOGGER SETUP --- //
def log = Logger.getLogger("com.acme.scriptrunner.SelectListUpdater")
log.setLevel(Level.DEBUG)
def optionsManager = ComponentAccessor.getOptionsManager()
def fieldLayoutManager = ComponentAccessor.fieldLayoutManager
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
// FIELD A -> Database Picker (source) id=31801
def fieldRelatedProjects = "Related Projects"
// FIELD B -> Select List (multiple choice) id=37200
def fieldRelatedProjectsList = "Related ProjectsList"
// ! FIELD A ###################################### :
def dbPickerCf = customFieldManager.getCustomFieldObjectsByName(fieldRelatedProjects).first()
def field_RP = getFieldById(dbPickerCf.id as String)
def selectedIds = field_RP.getValue()
log.debug("User selected IDs in DB picker: ${selectedIds}")
// ! FIELD B ###################################### :
// Set a fixed values FIELD B. TO DO: get from FIELD A
def selectListField = getFieldByName(fieldRelatedProjectsList)
//selectListField.setFormValue("P00014-OD Training")
//log.info("FIELD B) Valor conforme FIELD A) TO DO")
// --- Lookup description(s) for selected ID(s) ---
def descriptions = []
if (selectedIds) {
def ids = (selectedIds instanceof List) ? selectedIds : [selectedIds]
log.debug("ids: ${ids}")
DatabaseUtil.withSql("SGI") { db ->
ids.each { idVal ->
def row = db.firstRow("SELECT [I2SSGI].[dbo].[Projeto].[Nome] as nome FROM [I2SSGI].[dbo].[Projeto] JOIN [i2SSGI].[dbo].[ProjetoRefContabilistica] ON [i2SSGI].[dbo].[ProjetoRefContabilistica].[Id] = [I2SSGI].[dbo].[Projeto].[Id]where [I2SSGI].[dbo].[Projeto].EstadoId = 1 AND [I2SSGI].[dbo].[ProjetoRefContabilistica].[RefContabilistica] = ?", [idVal])
if (row?.nome) {
descriptions << row.nome.trim()
}
}
}
}
log.debug("Resolved descriptions: ${descriptions}")
// --- Set into FIELD B ---
if (descriptions) {
// For multi-select: pass list
selectListField.setFormValue(descriptions)
} else {
selectListField.setFormValue(null) // clear if nothing
}
>To Do/Help:
The "Related Projects List" , is possible to hide from user and the behaviour still works?
(I tried several things but it doesn't work: remove field from screen; include in behaviours selectListField.setHidden(true); Jira Adminintration\Issues\Field Configuration\Hide field)
Many thanks,
Isabel Fonseca