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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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
TAGS
Community showcase
Posted in Jira Software

Presenting the "Best of 2020" Jira Software roundup!

Catch up with Atlassian Product Managers in our 2020 Demo Den round-up! From Advanced Roadmaps to Code in Jira to Next-Gen Workflows, check out the videos below to help up-level your work in the new ...

7,184 views 8 28
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you