Hi there
I have a Scriptrunner Listener Script that updates some fields based on an event.
I have set the event to "Comment Edited", but it applies to other events too.
The script will...
I have made a video to demonstrate it:
https://drive.google.com/file/d/1AFBMXI2586AFd_6g2ppsfBCh4vr75Udw/view
Here is the script:
import org.apache.log4j.Level
log.setLevel(Level.DEBUG)
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogImpl2
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def worklogManager = ComponentAccessor.getWorklogManager()
def documentationField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Documentation")
Random rnd = new Random()
def myRandInt = rnd.nextInt(100)
def mutableIssue = issueManager.getIssueObject(event.issue.getId());
def worklog = new WorklogImpl2(mutableIssue, null, user.name, "My Comment", new Date(), null, null,myRandInt*3600, null)
worklogManager.create(user, worklog, myRandInt*3600, false)
log.debug("New Time Logged: " + myRandInt)
log.debug("Remaining: " + myRandInt)
mutableIssue.setEstimate(myRandInt*3600)
log.debug("Estimate: " + myRandInt)
def description = "Description: " + myRandInt
mutableIssue.setDescription(description)
log.debug("Description: " + description)
def documentation = "Documentation: " + myRandInt
mutableIssue.setCustomFieldValue(documentationField, documentation)
log.debug("Documentation: " + documentation)
//mutableIssue.store()
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
log.info("Issue "+mutableIssue.key+" updated")
There are no errors thrown and the logs are all correct.
This screenshot also shows you that the changes are protocolled in the history, but the (red) values are not written on the issue:
I am using Jira Server 7.7.1 and cannot upgrade at the moment as we are in the middle of a project.
Is this reproduceable on higher versions?
Hi @Leonard Chew ,
I reproduced your issue and was able to fix it by adding
import com.atlassian.jira.issue.index.IssueIndexingService
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
issue.store()
issueIndexingService.reIndex(issue)
at the end of the script.
Hope that works for you.
Antoine
Thanks for taking your time to reproduce it and your superfast answer!
Unfortunately reindexing the item did not solve the problem on my system. The behaviour is still the same.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Since you are using the mutableIssue, could you please use this script instead :
import com.atlassian.jira.issue.index.IssueIndexingService
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
mutableIssue.store()
issueIndexingService.reIndex(mutableIssue)
Note that you can just use issue in the case of a comment event.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I had substituted "issue" with "mutableIssue" and "event.issue", but in both cases it did not show any effect.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just make sure you are using the same object ("issue" or your created mutableIssue) throughout the script.
Otherwise I do not know, I was able to make it work with both on 7.6.0.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Is it possible that we are running the script in a different context?
You are speaking of an "issue" object, but on my Listener Script there is no predefined "issue" object.
I have event.issue, which cannot be updated and then the mutableIssue I create, but no "issue".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Most of the time you will be working on the "issue" object. Sure you are triggering an event, but this event happens on an issue (not always the case), which means you can use the issue object. Same goes for post functions.
In your case it should not matter since event.issue will return the same object. The script console is highlighting an error because you did not declare it (same as event).
So you can remove
def mutableIssue = ...
and just replace it with issue. Same for event.issue.
In this case it should not change the outcome but you can try.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've had the same problem trying to update issue priority via a listener based on custom field values entered during transitioning issue. The issue is only produced while using IssueService. The symptoms are as follows:
* Changing the custom field values on edit issue or view issue screen does update priority correctly.
* Changing the custom field values on a transition issue screen does produce an issue history log for priority change but does not actually change the priority value.
Using the IssueIndexingService does not help. Also, switching between different issue events do not matter as the listener is triggered correctly.
Running Jira 8+ I found that using IssueManager updateIssue method does the trick, the problem only occurs while trying to update the issue via Atlassian's advised way to update an issue - using IssueService and all the validations.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.