You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.
View groupJoin the community to find out what other Atlassian users are discussing, debating and creating.
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())}
}
}
}
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:
def changedCUSTOMFIELD = event?.getChangeLog()?.getRelated('ChildChangeItem')?.find {it.field.toString().substring(0,3) == 'CFN'}
if (changedCUSTOMFIELD || event.eventTypeId == EventType.ISSUE_CREATED_ID || issueType != '10000') {Is this correct?
Thanks,
Moga
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 ...
Connect with like-minded Atlassian users at free events near you!
Find an eventConnect with like-minded Atlassian users at free events near you!
Unfortunately there are no Community Events near you at the moment.
Host an eventYou're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.