Forums

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

How to update issue Description field using Custom Listener from ScriptRunner?

Birgir Pálsson September 25, 2019

I have a Custom Listener from ScriptRunner for the "Work Started on Issue" event implemented like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


def im = ComponentAccessor.getIssueManager();
def i = event.issue
def userManager = ComponentAccessor.getUserManager();
def issueToChange = im.getIssueObject(i.id)
def user = userManager.getUserByName(currentUser.name)

def iType = i.issueType.name

if(iType == "Imputation"){
issueToChange.setDescription("hmm")
im.updateIssue(user, issueToChange, EventDispatchOption.DO_NOT_DISPATCH, false)
}

The script runs without an error when issue is set to the "in Progress" state.

In the History tab for the issue there is a record of the change made on the Description field:

Capture.PNG

But the Description field it self is empty!

Any ideas?

(It is on a local JIRA server installation version 7.8.0)

Thanks,

Birgir

 

4 answers

0 votes
Jose Luis Casarrubias September 22, 2020

Hello,

You can try this one. 

I used it on my side and worked fine

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.user.ApplicationUser
import groovy.transform.Field
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

IssueEvent event = event as IssueEvent;
JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
ApplicationUser appUser = authContext.getLoggedInUser();
ComponentAccessor componentAccessor = new ComponentAccessor();
IssueManager issueManager = componentAccessor.getIssueManager();
MutableIssue issue = event.getIssue() as MutableIssue;
def issueIndexingService = componentAccessor.getComponent(IssueIndexingService);
String text = "hmm";

if(issue.getIssueType().name == "Imputation")
{
  issue.setDescription(text);
  issueManager.updateIssue(appUser, issue, EventDispatchOption.ISSUE_UPDATED, false);
  boolean wasIndexing = ImportUtils.isIndexIssues();
  ImportUtils.setIndexIssues(true);
  issueIndexingService.reIndex(issueManager.getIssueObject(issue.id));
  ImportUtils.setIndexIssues(wasIndexing);
}

I hope it helps.

Regards,

Luis

0 votes
Tuncay Senturk
Community Champion
September 26, 2019

Did you check your Jira index? Can there be any corruption?

Birgir Pálsson September 26, 2019

Yes I checked and it all looks fine.

Tuncay Senturk
Community Champion
October 6, 2019

Sorry that I could not help. I was just curious if you've found out the root cause.

Birgir Pálsson October 7, 2019

No I still haven´t figured this out :-)

0 votes
Birgir Pálsson September 26, 2019

Thanks for the suggestion. I did as you proposed but the results are the same.

I also tried to update the Summary field to see if this had something to do with the Description field, but that didn't update either.

 

Capture.PNG 

0 votes
Tuncay Senturk
Community Champion
September 25, 2019

That's odd,

Can you also try editing issue 

IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters(); 
issueInputParameters.setDescription("I am a new description");
UpdateValidationResult updateValidationResult = issueService.validateUpdate(user, i.id, issueInputParameters);
if (updateValidationResult.isValid()) {
IssueResult updateResult = issueService.update(user, updateValidationResult);
if (!updateResult.isValid()) {
// Do something
}
}

as described here

Suggest an answer

Log in or Sign up to answer