JIRA SERVER

Recently we had a request to synchronize data from the text custom field on one side into the select list on the other side. 

The scripts below help to send the text custom field value to the destination side and add a new option to the existing select list field if there is no such option yet. 

Sending side

Data Filter

// SETTINGS
final def textFieldCfName = "Text Custom Field Name"
// END SETTINGS

def localOptionValue = issue.customFields[textFieldCfName]?.value
if (localOptionValue == null) {
    throw new com.exalate.api.exception.IssueTrackerException(
    "Can not synchronize 
            `"+textFieldCfName+"` 
                to destination jira since there is no value set for issue 
           `"+ issueKey.URN +"` 
            ("+issueKey.id+"
    )"
  )
}
replica.customKeys."Text field Value" = localOptionValue
 

Receiving side

Create Processor

// SETTINGS
final def nygSelectListCfName = "Select List Custom Field Name" 
// END SETTINGS


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext

def cfm = ComponentAccessor.getCustomFieldManager()
def om = ComponentAccessor.getOptionsManager()

def remoteOptionValue = replica.customKeys."Text field Value" as String
def cf = cfm.getCustomFieldObjectsByName(nygSelectListCfName).find()
def fCfg = cf.getRelevantConfig(IssueContext.GLOBAL)
def localOptions = om.getOptions(fCfg)
if (!localOptions.any { o -> o.value == remoteOptionValue }) {
    //FieldConfig fieldConfig, Long parentOptionId, Long sequence, String value
    om.createOption(fCfg, null, 0L, remoteOptionValue)
}
 

Change processor

//INTENTIONALLY LEFT EMPTY - there should be no changes

In case an issue is not synchronized and you wand to trigger synchronization by updating the issue filed, you need to create a JQL trigger.

 

See Also