JIRA: Fill Comment Template on workflow transitions
Asked by patman_y:

Hola Atlassians,   

I have seen several cases of adding a postfunction and/or using ScriptRunner to add a comment automatically after doing a transition, but my case scenario is different.   

I have a workflow transition "Rejected", which moves a ticket's status to "Rejected"   
That transition screen has 2 fields - a "Rejection reason", and a "comment"   
Depending on the "Rejection reason", I want to populate the comment with a pre-defined template,  
AND I want to show an error if the comment was not edited (i.e. comment === comment_template)  
> ++**Example:**++   
> Rejection reason: Not reproducible  
> Comment template:
>
> ```
> """
> Status: Failed
> Error: Unable to reproduce the error.?
> Tested on version: {{VERSION}}
> """
> ```
>
> <br />
>
> and if the final comment matches the Comment Template, user shouldn't be able to go through the transition, and actually show an error "Comment not edited".

I would appreciate your support on that, hoping to find a proper solution for this problem.   
Thank you!


Answers:
author: Aditya Verma
body: My suggestion would be a simple one:  

On your "Reject" transition screen, just put 2 fields "Rejection reason" and "Tested on version"(do not put comment field here).

Once this is submitted, we can run an automation which will drop a dynamic comment on the ticket based on the values of these two fields.

Thanks,  
Aditya


author: Yavuz Selim
body: Hello [@patman_y](/t5/user/viewprofilepage/user-id/1805778) ,

I reached the community page through the advertisement you opened. I examined your request, it can be done with Behavior, but it will not be enough alone. Scripted Validator will also be required. Only then can the transition be allowed by checking whether the comment has been edited or not.  
If you would like to get professional support on this issue, please be clear in the comment and I can refer to your ad.

Best regards, Yavuz


author: Michael Bramhall
body: This would be possible in behaviours in scriptrunner. Pseduocode that should achieve your use case.

1. Create a behaviour associate with the correct projects and issue types that are applicable/or make global if system wide.
2. Tag "Rejection reason" in the behaviour. Add a script
3. Add a condition to the script that checks if present on the "reject" screen else this behaviour would trigger on each screen with "Rejection reason" present
4. Use the following line to get the value of rejection reason field when it changes  
   def rejectionReason = getFieldById(getFieldChanged()).getValue()
5. Define the template of the comment such as  
   def commentTemplate = "TODO"
6. Get the field for comments  
   def comment = getFieldByName("comments")
7. Use conditions with the above define variables such as  
   if(rejectionReason == "Example"){  
   comment.setFormValue(commentTemplate)  
   }  

   Hope this helps

comments: author: patman_y
body: hey [@Michael Bramhall](/t5/user/viewprofilepage/user-id/5425623)   

here's my code:   

```
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager

// Get the value of the rejection reason field
def rejectionReasonField = getFieldById(getFieldChanged())
def rejectionReason = rejectionReasonField.getValue() as String

// Prepare the template based on the rejection reason
def commentTemplate = "DEFAULT VALUE"

switch (rejectionReason) {
 case "Reason 1":
  commentTemplate = """
   Status: Rejected
   Reason: Lorem ipsum 1
  """.trim()
  break
 case "Reason 2":
  commentTemplate = """
   Status: Rejected
   Reason: Lorem ipsum 2
  """.trim()
  break
  default:
   commentTemplate = "No specific reason provided."
}

// Set the Comment field value with the template
def commentField = getFieldById("comment")
commentField.setFormValue(commentTemplate)
```

<br />

and yet, the commentField doesnt get modified at all, even with the DEFAULT VALUE.   

Am I doing something wrong?


author: Samuel Gatica
body: Hi [@patman_y](/t5/user/viewprofilepage/user-id/1805778)

Welcome to the community!

Regrettably, this is not achievable using the native Jira tools.

You may be able to archive this with the [Behaviours - ScriptRunner for Jira Cloud](https://marketplace.atlassian.com/apps/1228522/behaviours-scriptrunner-for-jira-cloud?tab=overview&hosting=cloud) app.

Best regards

Sam

comments: author: patman_y
body: Hey [@Samuel Gatica](/t5/user/viewprofilepage/user-id/753028) ! thank you for your reply  

How would this be done with ScriptRunner Behaviours? I tried ChatGPT'ing it, but to no avail, unfortunately


Tags: behaviours

comment

data-center

jira-data-center

post-function

scriptrunner

transition

user-experience

user-interface