Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Published in Marketplace Apps & Integrations

Do Not Lose your Customer’s Trust

Missing deadlines is one of the biggest problems every team lead wants to avoid when dealing with managed services. When the customer contracts your company to help with IT services it is expected th...

24 views 0 0
Read article

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