Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Community showcase
Published in Jira Service Management

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

270 views 8 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you