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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,431
Community Members
 
Community Events
176
Community Groups

Scriptunner, create a listener that will update description with latest comment

I'm trying to create a listener that will update description with latest comment. Below are my codes.

when the listener triggered, the description are still not updated with the last comment 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.bc.issue.IssueService

//Get some component
IssueManager im = ComponentAccessor.getIssueManager()
IssueService issueService = ComponentAccessor.getIssueService();
MutableIssue issue = im.getIssueObject("DP-30")

if(issue){

//Fetch latest comment
def commentManager = ComponentAccessor.commentManager
def comment = commentManager.getLastComment(issue)



if (comment) {
comment.body
issue.setDescription(comment.body)
}



}

else {

return "Issue doesn't exist"
}

 

2 answers

1 accepted

2 votes
Answer accepted
Kevin Johnson Community Leader Aug 30, 2020

@Sharif Ismail Hope this could help. Create a Listener like thisupdate.PNGUse this code to Update the Description with Author name, Time&Date, Comment body

import com.atlassian.jira.component.ComponentAccessor

// Get some components
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueService = ComponentAccessor.issueService

// Set variables
def issue = event.issue
def issueId = issue.id
def comment = event.getComment()
def commentBody = event.getComment().getBody()

def issueInputParameters = issueService.newIssueInputParameters()

if(issue.getDescription() != null){
issueInputParameters.setDescription(issue.getDescription()+"\n"+ comment.getAuthorFullName() + " - " + comment.getCreated() +"\n"+ commentBody);
}
else{
issueInputParameters.setDescription(comment.getAuthorFullName() + " - " + comment.getCreated() +"\n"+ commentBody);
}

def updateValidationResult = issueService.validateUpdate(loggedInUser, issueId, issueInputParameters)

assert updateValidationResult.isValid() : updateValidationResult.errorCollection

def issueResult = issueService.update(loggedInUser, updateValidationResult)

assert issueResult.isValid() : issueResult.errorCollection

Thank you kevin johnson , it works like a charm

I have a few questions:

1.I'm new towards scriptrunner , so each time I want to try out the features I would test my codes in the console , for your codes I did the same , but there was lot of errors . But when I tried it in the listener , It doesn't have any problem and works finely. Do you know why ?

2.Starting from the line of declaring updateValidationResult as a variable until the last line of the your code, correct me if im wrong but from my understanding what you did is trying to validate each changes of any field in issue . right ?

why and when should I use this validation process ? 

Kevin Johnson Community Leader Aug 30, 2020

1. I'm assuming that you mean you have tried this in the console of Scriptrunner Plugin, the thing is, the code works in this Listener mainly with something known as the event, since the event is not valid in the console, it throws an error, if you paste the code in the console, you can clearly see the errors starting to show off only from the event-part.

2. Almost correct, here in the updateValidationResult the parameters given inside are checked if they are valid and updated to the issue, if not to throw an error using assert, once the parameters are valid, the issue gets updated and the result is validated.

You can use validation as a precaution where ever you feel like there might be an error. 

0 votes

Out of curiosity, why?

Why would you want to do this horrible thing to your users?  If you genuinely do have some strange need to stuff all the commentary on an issue into the description, why not just turn off the permission to comment and train your users to edit the description when they want to add a comment?

I'm a newbie towards scriptrunner , as I don't have any on going project in JIRA. I try to simulate some situation for me to exercise and practice on Scriptrunner, so this is one of my simulation sir

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events