Scriptrunner Listener for only specific custom field edited on Issue Updated Event

Kmmaughs
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 Leaders.
January 14, 2021

Hello, I have a listener set up to change the Epic Link and FixVersion on an issue when a custom field is changed. This is set up for a specific project and should be fired on the Issue Updated Event. However, the Epic Link and FixVersion are being changed when any field is edited and not my specific custom field. I'm wondering where the issue could be. Here's a little sample of what I've got:

import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.event.issue.field.CustomFieldUpdatedEvent


def bDebug = false; // This is a debug flag
def bForced = false;
def strUser = event.getUser().username.toLowerCase();

if (strUser == 'test_username_here'){
bForced = false;
bDebug = true;
}

if (bDebug) {UserMessageUtil.info('Entering the Listener Script...')}
def issue = event.getIssue();
def issueType = issue.getIssueType().id
def issueManager = ComponentAccessor.getIssueManager()
def componentIssue = issueManager.getIssueObject('')

if (bDebug) {UserMessageUtil.info('Have issue: ' + issue.getSummary())}
def changedCUSTOMFIELD = event?.getChangeLog()?.getRelated('ChildChangeItem')?.find {it.field.toString().substring(0,3) == 'CFN'}
if (bForced) {changedCUSTOMFIELD = true}
if (bDebug) {UserMessageUtil.info('Found changed CFN... ')}
if (changedCUSTOMFIELD || event.eventTypeId == EventType.ISSUE_CREATED_ID || issueType != '10000') {
if (bDebug) {UserMessageUtil.success('CUSTOM FIELD has changed... do something about it!')}
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def mlcs = null //The Custom field is a Multi-Level Cascading Select

//////////////////////////////////
//Specify the CF List to be used//
//////////////////////////////////

String[] mlcsValuesList
try { 'Custom Field MLCS'
mlcs = customFieldManager.getCustomFieldObject('customfield_10000')
ArrayList<String> mlcsValues = (ArrayList<String>) issue.getCustomFieldValue(mlcs)
mlcsValuesList = mlcsValues.toArray()
} catch(Exception e) {}


if (mlcs != null) {
//Display messages to the user of the values
try {
if (bDebug) {if (mlcsValuesList[0] != null) {UserMessageUtil.success('Level 0 = ' + mlcsValuesList[0])}}
if (bDebug) {if (mlcsValuesList[1] != null) {UserMessageUtil.success('Level 1 = ' + mlcsValuesList[1])}}
} catch (Exception ex) {
UserMessageUtil.error('Error: ' + ex.getMessage() + '\n' + ex.getStackTrace())
}

def strEpicKey = ''

//////////////////
// GET EPIC KEY //
//////////////////

if (mlcsValuesList[0] == 'Option 1') {
strEpicKey ='ISSUEKEY-1';
}
if (mlcsValuesList[0] == 'Option 1' && mlcsValuesList[1] == 'Sub-Option 1') {
strEpicKey ='ISSUEKEY-2';

//////////////////////////
//set Epic Link of Issue//
//////////////////////////

try {
if (bDebug) {UserMessageUtil.info('Setting Epic Link to ' + strEpicKey)}
def epicIssue = issueManager.getIssueObject(strEpicKey) // Undeclared Epic
def epicLink = customFieldManager.getCustomFieldObjectsByName('Epic Link')[0]
def versionManager = ComponentAccessor.getVersionManager()
def project = issue.getProjectObject()
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())
if (bDebug) {UserMessageUtil.info('Epic Link set to = ' + strEpicKey)}
} catch (Exception ex) {
if (bDebug) {UserMessageUtil.error('Error in Epic link ' + strEpicKey + ': ' + ex.getMessage() + '\n' + ex.getStackTrace())}
}

///////////////////////////
//Set fixVersion of Issue//
///////////////////////////

try {
def targetVersion = issueManager.getIssueByCurrentKey(strEpicKey).getFixVersions()[0]
if (bDebug) {UserMessageUtil.info('Setting fix Version to ' + targetVersion)}
def field = ComponentAccessor.fieldManager.getOrderableField('fixVersions')
field.updateValue(null, issue, new ModifiedValue(issue.fixVersions, [targetVersion]),new DefaultIssueChangeHolder())
if (bDebug) {UserMessageUtil.success('Fix Version set to = ' + targetVersion.toString())}
} catch (Exception ex) {
if (bDebug) {UserMessageUtil.error('Error in fixVersion for ' + strEpicKey + ': ' + ex.getMessage() + '\n' + ex.getStackTrace())}
}
}
}

 

 

1 answer

0 votes
mogavenasan
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 Leaders.
January 18, 2021

Hi @Kmmaughs,

It would be great if the provided code has some sort of indentation.

I will try to give some steps to narrow down the troubleshooting:

  1. Putting a DEBUG logging on this line and also checking the result will be helpful.
    def changedCUSTOMFIELD = event?.getChangeLog()?.getRelated('ChildChangeItem')?.find {it.field.toString().substring(0,3) == 'CFN'}
  2. For the line below, they are OR conditions and you have issueType != '10000':
    if (changedCUSTOMFIELD || event.eventTypeId == EventType.ISSUE_CREATED_ID || issueType != '10000') {
    Is this correct?

Thanks,
Moga

Suggest an answer

Log in or Sign up to answer