Issue does not get updated

IT Infrastructure & Systems February 6, 2020

I am trying to update a custom field on an issue after a transition. I created  a post function with the following code running. It does not throw an error message, when I log it seems to fill the custom field, but in the end nothing happens with the issue.

 

What am I doing wrong?

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParametersImpl;
import java.util.Date.*
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def outageStart = customFieldManager.getCustomFieldObject('customfield_12631');
def outageEnd = customFieldManager.getCustomFieldObject('customfield_12632');
def outageDuration = customFieldManager.getCustomFieldObject('customfield_13032');


if(issue.getCustomFieldValue(outageStart) && issue.getCustomFieldValue(outageEnd)) {
def dateValue = issue.getCustomFieldValue(outageStart) as Date
def dateValue2 = issue.getCustomFieldValue(outageEnd) as Date

issue.setCustomFieldValue(outageDuration, ((dateValue2.getTime()-dateValue.getTime())/1000/60))
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)

boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
ImportUtils.setIndexIssues(wasIndexing);
}

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 6, 2020

Which position in the list of postfunctions on your transition is this script placed?

My guess is that you have it set before the "Update change history for an issue and store the issue in the database".

What I suspect is happening is that your script updates the issues in the background, but then the postfunction stores an older version of the issue object (without your fields populated) and overwrites those values. If you look in the history, you might see evidence of that.

Your script would be perfect for a script listener because it is compeltely stand-alone.

In a post function, it's better to use the "issueInputParameters" object to set the custom field values. Then, these will be picked up by the built-in functions to store and index the issue.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import java.util.Date.*

def customFieldManager = ComponentAccessor.customFieldManager

def outageStart = customFieldManager.getCustomFieldObject('customfield_12631')
def outageEnd = customFieldManager.getCustomFieldObject('customfield_12632')
def outageDuration = customFieldManager.getCustomFieldObject('customfield_13032')

if(issue.getCustomFieldValue(outageStart) && issue.getCustomFieldValue(outageEnd)) {
def dateValue = issue.getCustomFieldValue(outageStart) as Date
def dateValue2 = issue.getCustomFieldValue(outageEnd) as Date

issueInputParameters.addCustomFieldValue(outageDuration.id, ((dateValue2.getTime()-dateValue.getTime())/1000/60))

Or, keep your code exactly as is, but move it after the store and re-index post function tasks. This will cause additional db and indexing transaction and is generally less efficient, but will work in a pinch.

IT Infrastructure & Systems February 6, 2020

It is the second to last, so behind "Update change history for an issue and store the issue in the database".

 

I am going to try out your script, I am just getting some errors with the last line that I am trying to fix.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 7, 2020

If you leave it second to last, you will need a script more like what you had before and throw some debug messages to figure out why it wasn't working.

But if you try my version, you will need to move the function up.

And you can just add a toString() on that last line since the addCustomFieldvalue expects a string value 

issueInputParameters.addCustomFieldValue(outageDuration.id, ((dateValue2.getTime()-dateValue.getTime())/1000/60).toString())
TAGS
AUG Leaders

Atlassian Community Events