Why doesn't Script Listener write to Issue History?

Bruce Mohler September 10, 2019

We have a Script Listener that updates fields but doesn't update the History of the Issue.  Per Payne on an earlier question "...EVERY change (except comments and changes to comments) SHOULD be recorded in the History tab."

This is a snippet of the Groovy code:

---------------------------------------------------------------------------------------

/*
* Fires when a Issue Link is created.
* Exits if Linked to Issue is not Initiative / Feature / Epic
* Copies Custom Fields down from Linked Issue
*/

// imports...

// Setup Custom Field Manager
def CustomFieldManager = ComponentAccessor.getCustomFieldManager()

// Grab Event
def event = event as IssueLinkCreatedEvent

// sourceIssue is really the Issue that is being Linked to.
def sourceIssue = event.getIssueLink().getSourceObject()
log.info("sourceIssue: " + sourceIssue.key)

// Exit if not Initiative / Feature / Epic
if (sourceIssue.issueType.name != "Epic" &&
sourceIssue.issueType.name != "Feature" &&
sourceIssue.issueType.name != "Initiative") {
log.info("Exiting ... sourceIssue type of: " + sourceIssue.issueType.name + " is not Epic / Feature / Initiative")
return 0
}

// Which makes the Destination object really the issue
def issue = event.getIssueLink().getDestinationObject()
log.info("issue: " + issue.key)

/**
* Workfront Project
*/

def CfWorkFrontProject = CustomFieldManager.getCustomFieldObject('customfield_10703')
log.info("CfWorkFrontProject")

// Copy if target Field IS Empty and source field is NOT Emtpy
if(!issue.getCustomFieldValue(CfWorkFrontProject) &&
sourceIssue.getCustomFieldValue(CfWorkFrontProject)
) {
def oldData = issue.getCustomFieldValue(CfWorkFrontProject)
def newData = sourceIssue.getCustomFieldValue(CfWorkFrontProject)

CfWorkFrontProject.updateValue(null, issue, new ModifiedValue(oldData, newData), new DefaultIssueChangeHolder());
log.info("Workfront Project updated to: " + newData)
} else {
log.info ("Not CfWorkFrontProject && not value in CfWorkFrontProject")
}

return

---------------------------------------------------------------------------------------

When the listener is triggered, it does update the CfWorkFrontProject field but there's no corresponding log entry and we have no clue why.

Are we updating custom fields incorrectly?

My colleague and I are very new to Jira/ScriptRunner/Groovy and so may be writing some very offensive code.  For that we apologize.

1 answer

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 11, 2019

Hi @Bruce Mohler ,

You can use this code to have the update display in the history : 

import com.atlassian.jira.event.type.*
import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def sendMail = true
int cfId = 13401
def cfObject = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(cfId)
def cfNewValue = ....

issue.setCustomFieldValue(cfObject, cfNewValue)
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, sendMail)

Antoine

Bruce Mohler September 11, 2019

Thank you, Antoine.  I'll try that this morning.

Bruce Mohler September 11, 2019

Comment removed.

Bruce Mohler September 11, 2019

Antoine, we have some scripts which are using the IssueService rather than the IssueManager to change the values of custom fields (and they're not updating History either).

We're not really clear what the canonical way to update these fields is.

Bruce Mohler September 11, 2019

@Antoine Berry , when I paste those lines into the Script Console, the following 2 lines generate syntax errors:

def cfObject = getCustFieldObjectById(cfId)
issue.setCustomFieldValue(cfObject, cfNewValue)

The first can't find a method that accepts an int as an argument.

The second can't find a method that accepts an Object and String as arguments.

I'm a bit baffled.  I'm not sure whether I'm missing an import or 2?  I tried to search Google for getCustFieldObjectById (hoping to land in the Atlassian API documentation) but it kept trying to send me to another method (getCustomFieldObjectByName).

Suggestions?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 12, 2019

Hi @Bruce Mohler ,

I am terribly sorry, that is a custom method I am using and forgot to replace it. I updated the original answer with the correct code. It should work now if the cfNewValue fits to the custom field type.

IssueService is AFAIK the best way to update as you can check if the field can correctly be updated, so you can use that as well. 

Antoine

Bruce Mohler September 12, 2019

Thank you, @Antoine Berry

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 12, 2019

Hi @Bruce Mohler , did that work for you ? In that case please make sure to accept the answer so it can help others in the future.

Thierry Dalon August 4, 2022

Unfortunately it seems not to work for me. The field is updated but I have no change visible in the history :-( 

Also an IssueUpdated event isn't triggered.

Thierry Dalon August 9, 2022

Finally I have come to a working solution based on the issueService. I have shared my learnings here.

Suggest an answer

Log in or Sign up to answer