Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,294,509
Community Members
 
Community Events
165
Community Groups

Issue does not get updated

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

0 votes

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 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.

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())

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

261 views 0 6
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