Scriptrunner Script not updating native fields when triggerd by workflow post function

Leonard Chew
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.
July 11, 2019

Hi there

I have a Scriptrunner Listener Script that updates some fields based on an event.
I have set the event to "Comment Edited", but it applies to other events too.

The script will...

  1. ... correctly update all fields, if the event is fired by the native action of editing the comment.
  2. ... correctly update custom fields and the work logged, but NOT update the native fields, if the event is fired by a postfunction on a workflow transition.

I have made a video to demonstrate it:
https://drive.google.com/file/d/1AFBMXI2586AFd_6g2ppsfBCh4vr75Udw/view

Here is the script:

import org.apache.log4j.Level
log.setLevel(Level.DEBUG)

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogImpl2

def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def worklogManager = ComponentAccessor.getWorklogManager()

def documentationField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Documentation")

Random rnd = new Random()
def myRandInt = rnd.nextInt(100)

def mutableIssue = issueManager.getIssueObject(event.issue.getId());
def worklog = new WorklogImpl2(mutableIssue, null, user.name, "My Comment", new Date(), null, null,myRandInt*3600, null)
worklogManager.create(user, worklog, myRandInt*3600, false)
log.debug("New Time Logged: " + myRandInt)
log.debug("Remaining: " + myRandInt)

mutableIssue.setEstimate(myRandInt*3600)
log.debug("Estimate: " + myRandInt)

def description = "Description: " + myRandInt
mutableIssue.setDescription(description)
log.debug("Description: " + description)

def documentation = "Documentation: " + myRandInt
mutableIssue.setCustomFieldValue(documentationField, documentation)
log.debug("Documentation: " + documentation)

//mutableIssue.store()
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

log.info("Issue "+mutableIssue.key+" updated")


There are no errors thrown and the logs are all correct.

This screenshot also shows you that the changes are protocolled in the history, but the (red) values are not written on the issue:

issue-screenshot.png

I am using Jira Server 7.7.1 and cannot upgrade at the moment as we are in the middle of a project.
Is this reproduceable on higher versions?

2 answers

2 votes
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.
July 11, 2019

Hi @Leonard Chew ,

I reproduced your issue and was able to fix it by adding 

import com.atlassian.jira.issue.index.IssueIndexingService

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)

at the end of the script.

Hope that works for you.

Antoine

Leonard Chew
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.
July 11, 2019

Thanks for taking your time to reproduce it and your superfast answer!

Unfortunately reindexing the item did not solve the problem on my system. The behaviour is still the same. 

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.
July 11, 2019

Since you are using the mutableIssue, could you please use this script instead : 

import com.atlassian.jira.issue.index.IssueIndexingService

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
mutableIssue.store()
issueIndexingService.reIndex(mutableIssue)

Note that you can just use issue in the case of a comment event.

Leonard Chew
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.
July 11, 2019

I had substituted "issue" with "mutableIssue" and "event.issue", but in both cases it did not show any effect.

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.
July 11, 2019

Just make sure you are using the same object ("issue" or your created mutableIssue) throughout the script. 

Otherwise I do not know, I was able to make it work with both on 7.6.0.

Leonard Chew
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.
July 16, 2019

Is it possible that we are running the script in a different context?

You are speaking of an "issue" object, but on my Listener Script there is no predefined "issue" object. 

I have event.issue, which cannot be updated and then the mutableIssue I create, but no "issue".

issue.png

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.
July 16, 2019

Most of the time you will be working on the "issue" object. Sure you are triggering an event, but this event happens on an issue (not always the case), which means you can use the issue object. Same goes for post functions.

In your case it should not matter since event.issue will return the same object. The script console is highlighting an error because you did not declare it (same as event).

So you can remove

def mutableIssue = ... 

and just replace it with issue. Same for event.issue.

In this case it should not change the outcome but you can try.

1 vote
Kris Voog March 20, 2022

I've had the same problem trying to update issue priority via a listener based on custom field values entered during transitioning issue. The issue is only produced while using IssueService. The symptoms are as follows:

* Changing the custom field values on edit issue or view issue screen does update priority correctly.

* Changing the custom field values on a transition issue screen does produce an issue history log for priority change but does not actually change the priority value.

Using the IssueIndexingService does not help. Also, switching between different issue events do not matter as the listener is triggered correctly.

Running Jira 8+ I found that using IssueManager updateIssue method does the trick, the problem only occurs while trying to update the issue via Atlassian's advised way to update an issue - using IssueService and all the validations.

Suggest an answer

Log in or Sign up to answer