Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Correct way of updating an issue's field via a custom ScriptRunner listener and saving to DB?

Abi September 23, 2023

So here is the script. I omitted some code, but ideally this is how it is. My main issue is that when I run the "issueManager.updateIssue" function the issue's due date does not get updated. However in the history, it does say the change value was update. 

 

Is this the proper to way to do this?

 

//manager to manage ineractions with issue

IssueManager issueManager = ComponentAccessor.issueManager

//user context of who is working on this ticket

ApplicationUser user = ComponentAccessor.getUserManager().getUserByKey("issuebot")

//the database view of an issue

Issue nonMutableIssue = event.getIssue()

MutableIssue mutableIssue = issueManager.getIssueObject(event.issue.getKey())



//print orignal dute date before change

//make the change

c.setTime(*SOME RANDOME TIME SET*)

String dateStr = (c.get(Calendar.MONTH) + 1) + "/" + c.get(Calendar.DAY_OF_MONTH) + "/" + c.get(Calendar.YEAR)

log.info TAG + "${mutableIssue.getKey()} due date set to: ${dateStr}"

mutableIssue.setDueDate(new Timestamp(c.getTimeInMillis()))

//updates the changes to the issue in the database
def updatedIssue = issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)


//run a reindex to get the latest changes made from the database
event.issue.reindex();



 

2 answers

0 votes
Hauke Bruno Wollentin
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.
September 24, 2023

I used the base part of your code to reproduce this, but in our instance (Jira DC 9.9.1) this works:

Screenshot (12).png

Screenshot (13).png

The actual code:

import java.sql.Timestamp

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()

MutableIssue issue = event.getIssue() as MutableIssue

Timestamp newDueDate = Timestamp.valueOf("2023-10-23 10:10:10.0");

issue.setDueDate(newDueDate)

def user = Users.getLoggedInUser() as ApplicationUser

issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Does "the update don't go into the DB" mean, that in your issue view the due date field is incorrect and not set to the value that your listener should set?

Do you see any log lines in atlassian-jira.log about that?

Abi September 24, 2023

Thank you for taking the time and helping me out.

I looked more into the problem and discovered these two errors:

[o.c.g.runtime.m12n.MetaInfExtensionModule] Module [Innovalog Groovy extensions for JIRA] - Unable to load extension class [org.codehaus.groovy.runtime.SqlGroovyMethods] 
 [c.a.j.cache.request.RequestCacheRecorderImpl] Invalid use of RequestCache by thread: default-application-role-manager-cache-refresh.Incorrect usage of JIRA API.

After a little more research I found out that the plugin "JSU automation suite for jira workflows" is the reason why I could not update the fields to the database. Once I disabled the plugin, the fields were updated to the database. It seems like JSU is causing something to mess up when the issue gets created.

Like # people like this
Hauke Bruno Wollentin
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.
September 24, 2023

Wow, great catch. Than it's up to the plugin vendor I'd say :) 

0 votes
Fabio Racobaldo _Herzum_
Community Champion
September 23, 2023

Hi @Abi and welcome,

your code seems correct infact you see due date field correctly updated in the issue history.

Based on issue history, is due date updaed again? If so, there is something else that happens after your operation.

Fabio

Abi September 24, 2023

Thank you for the quick response. 

Based on the issue history, the due date does not get updated again. There are no other listeners/post-functions that run after the listener. 

A little more context. This custom listener runs when the issue is created. The due date is initially set when the issue is created by the user. I can see that in the database when I go to view the record of it. Then the listener runs, and updates the due date's value. But the value is not written to the database. 

The Issue object returned from "IssueManager.updateIssue()" should ideally be the reindexed and stored updated issue. But its not the same as "event.issue" object, which ideally should be. 

I have read about google and tried the HAPI way of updating the issue, but even that doesnt work!!! 

I even tried using the other way of "IssueManager.updateIssue()" passing in a

"UpdateIssueRequest.builder().build()" - I think skips all the permission checks. 
Like Hauke Bruno Wollentin likes this
Abi September 24, 2023

Based on the Issue history, there are no transactions that show the due date being removed. The due date is original set by the issue creator. When the customer listener runs when the issue gets created, the original due still stays the same even after a transaction log of the due date is being shown. 

I have exhausted all attempts of using "IssueManager.updatedIssue" function and even using the other method of passing in a "

UpdateIssueRequest.builder().build())" object. 
The initial issue created is stored in the database, however it seems like customer listener just doesn't push it though.

Suggest an answer

Log in or Sign up to answer