Scriptrunner - Post-function - jql doesn´t detect transitioned issue in new (destination) status

Stefan Salzl
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 9, 2022

Hi Community,

I try to implement a workflow post-function with scriptrunner: 


Given:

  • an issue of type course in status "In Planning"

When:

  • course issue is transitioned from "In Planning" to "Done"

Then:

  • collect all course issues in done with jql
  • sum value of field Course costs of all the issues collected with the above JQL

The point is: the post-function/JQL doesn´t detect the issue that triggers it.

eg. let´s say I have "Issue B" already in status done and "Issue A" currently in status "In Planning" --> when "Issue A" gets transitioned to "Done" (which correctly fires the post function) the jql within the post-function still only detects "Issue B" (I would expect the JQL to return Issue A and Issue B).

My question is:
Isn´t that possible with post-function? Is the post-function firing faster than the transition is processed?

I even tried to shift down the post-function in the list:

image.png

Any help or hints appreciated.

Best
Stefan

3 answers

2 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 14, 2022

> let´s say I have "Issue B" already in status done and "Issue A" currently in status "In Planning" --> when "Issue A" gets transitioned to "Done" (which correctly fires the post function) the jql within the post-function still only detects "Issue B" (I would expect the JQL to return Issue A and Issue B).

Ok, yes, there's a problem here - until the transition is complete (meaning everything has finished, including all the post-functions) Jira doesn't always have a definitive picture of the issue in its final shape.  This is not because there's some special flag or something, it's because the issue is effectively cached until Jira knows that everything has worked.  It doesn't actually get the final write to the database or even index, until the process has finished without error.

Two solutions to this are

  • Move your code into a listener - even though the events they catch are fired by post-functions, they don't actually run until the transition is complete.
  • Include the issue explicitly.  Your "run JQL" code returns a simple list of issues, nothing more.  So add a line of code that adds the current issue (issue A) to the list before you process the issues.each bit.
Stefan Salzl
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 15, 2022

Hi @Nic Brough -Adaptavist- 

thanks for your reply. I was thinking of indexing problems. It was just confusing for me as there is a seperate re-index post-function and I placed my scripted post-function after the re-index. 

Furthermore I added following line to my script:

log.info("trigger-issue: " + issue.key + " status: "+issue.status.name)

which returns:

trigger-issue: SRT-15 status: Done

 

So still (as the script returns that my trigger issue is in status "Done") it doesn´t mean it´s REALLY indexed with status done? :D

 

Best
Stefan

0 votes
Answer accepted
DasJo June 9, 2022

Hello Stefan,

Could you post your custom script maybe there is still a possibility in the script. In the simplest case a sleep of 20 seconds could be inserted.
Otherwise I would think of running the query via REST. I have the feeling REST is "faster" but also more programming effort.

Greeting,

Josef

Stefan Salzl
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 13, 2022

Hi @DasJo ,

This is my script:

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.web.bean.PagerFilter

def issueService = ComponentAccessor.issueService
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager


//getting customField objects for field budget and costs
def cfCourseCosts = customFieldManager.getCustomFieldObject("customfield_13400")
def cfBudget = customFieldManager.getCustomFieldObject("customfield_13303")

//get Issue (Budget Subtask) "Verbrauchtes Budget"
MutableIssue doneBudgetIssue = issueManager.getIssueByCurrentKey("BES-19")

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def searchService = ComponentAccessor.getComponentOfType(SearchService)

// The JQL query you want to search with
final jqlSearch = "project = BES and issuetype=Course and status = Done"

// Parse the query
def parseResult = searchService.parseQuery(user, jqlSearch)
if (!parseResult.valid) {
log.error('Invalid query')
return null
}

try {
// Perform the query to get the issues
def cumulativeDoneCourseCosts = (Double) 0
def results = searchService.search(user, parseResult.query, PagerFilter.unlimitedFilter)
def issues = results.results
log.warn("issues: "+issues)
// log.info("result: " + issues)
issues.each {
// log.info(it.key)
def costs = (Float) it.getCustomFieldValue(cfCourseCosts)
log.warn(costs)
cumulativeDoneCourseCosts = cumulativeDoneCourseCosts + costs
log.info("current cumulative: " + cumulativeDoneCourseCosts)

}
log.info("final calculation of course costs: " + cumulativeDoneCourseCosts)
// issues*.key
//set value of budget field to newly calculated/current budget
doneBudgetIssue.setCustomFieldValue(cfBudget, cumulativeDoneCourseCosts)

//update issue
issueManager.updateIssue(user, doneBudgetIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
//log.info(costArray)

} catch (SearchException e) {
e.printStackTrace()
null
}

 

Regarding your input it seems to me that a wait will be more accurate as it seems it even works "too fast". An API call that is event faster would lead in the wrong direction i guess.

Best
Stefan

Stefan Salzl
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 13, 2022

Hi @DasJo 

tried a sleep in my script - that didn´t solve the problem. still the jql misses the issue that triggered the script. 

eg:

there are 2 issues in status "Done":
Issue A
Issue B

Issue C is currently in status "In Planning" and is transitioned to "Done".

The workflow postfunction should now detect all issues in status "Done".

Nevertheless Issue C (which the post function was triggered by) is not detected.

 

Best
Stefan

DasJo June 13, 2022

A simple workaround could be that you take the field value of the current issue in addition to the JQL search. In scriptrunner you can read the triggering issue.

I'm not completely satisfied with the solution yet but it could be a first workaround.

Like Stefan Salzl likes this
Stefan Salzl
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 13, 2022

this could be a feasable workaround. Thanks for the hint ;)

Stefan Salzl
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 15, 2022

Hi @DasJo 

I added your suggestion and (even if I´m not a fan of workarounds): It does exactly what it should. 

Thanks for leading me into the right direction.

 

Best
Stefan

0 votes
Stefan Salzl
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 13, 2022

@Nic Brough -Adaptavist- 

any hints from you? Isn´t it possible that the previously transitioned issue is detected by the jql?

--> when I transition Issue C to "Done" and a jql in post-function is searching for issues in status done currently Issue C is not detected by my jql. Isn´t the transition fully processed at the point of post function processing?

Best
Stefan

Suggest an answer

Log in or Sign up to answer