Script to copy existing comments from parent issue to newly created sub-task
Asked by Manoj Gangwar:

Hello All,

When a subtask created then it should copy all the existing comments from the Parent issue. Also, I want to exclude comments made by Automation user.

I got an script but its showing error. Can some one help me with the correct script.

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

def subtask = issue // Newly created subtask
def parentIssue = subtask.getParentObject() // Parent issue

// Get the automation user (change 'Automation User' to your actual automation user's name)
def automationUser = ComponentAccessor.getUserManager().getUserByName('Automation User')

// Get comments from the parent issue
def comments = parentIssue.getComments()

// Loop through comments and copy to the subtask, excluding comments made by the automation user
comments.each { comment ->
if (comment.getAuthor() != automationUser) {
ComponentAccessor.getCommentManager().create(subtask, comment.getAuthor(), comment.getBody(), comment.getGroupLevel(), comment.getRoleLevelId(), comment.getCreated(), null)
}
}
```


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Manoj Gangwar](/t5/user/viewprofilepage/user-id/4484987)

Could you please share the error message you are currently getting along with a screenshot of your Automation configuration?

Instead of using Automation, why not do it directly on the workflow's Post-Function using ScriptRunner?

I am looking forward to your feedback and clarification.

Thank you and Kind regards,  
Ram

comments: author: Manoj Gangwar
body: [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) I am getting errors as in the attached screenshot. I can add it to the post function just looking for correct one.![sceensh.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/298104iFDF37B74989C9103/image-size/large?v=v2&px=999 "sceensh.PNG")


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Rebekka Heilmann _viadee_](/t5/user/viewprofilepage/user-id/5182395)

For your requirement, I suggest creating a Post-Function in the Create transition.

Below are the steps for your reference:-

1. Go to the Create transition and add a Post-Function as shown in the screenshot below:-

![configuration1.png](https://community.atlassian.com/t5/image/serverpage/image-id/298199iD53ECB42D246E1A0/image-size/large?v=v2&px=999 "configuration1.png")

2. Add a Post-Function as shown below:-

![configuration2.png](https://community.atlassian.com/t5/image/serverpage/image-id/298201iDB5F925EEC9F4C27/image-size/large?v=v2&px=999 "configuration2.png")

3. Select ScriptRunner's Custom Script Post-Function, as shown in the screenshot below:-![configuration3.png](https://community.atlassian.com/t5/image/serverpage/image-id/298202i072640887DFE496F/image-size/large?v=v2&px=999 "configuration3.png")

Once you are on the Custom Script Post-Function page, add the sample code below:-  

```
if (issue.isSubTask()) {
   def parent = issue.parentObject
    issue.set {
        parent.comments.each {
            issue.addComment(it.body)
        }
    }
}
```

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Custom Script Post-Function configuration:-

![configuration4.png](https://community.atlassian.com/t5/image/serverpage/image-id/298200i8D9CC8863AB98CB4/image-size/large?v=v2&px=999 "configuration4.png")

Save and Publish the Post-Function.

Note: Please ensure the Post-Function is added below the Issue Created Post-Function as shown in the Screenshot Below:-

![configuration5.png](https://community.atlassian.com/t5/image/serverpage/image-id/298204i164BE027B6B9FBFE/image-size/large?v=v2&px=999 "configuration5.png")

Below are a couple of test screenshots for your reference:-

1. Below is an example Story issue with some comments added to it.

![test1.png](https://community.atlassian.com/t5/image/serverpage/image-id/298203iC379E91C7FB1EB08/image-size/large?v=v2&px=999 "test1.png")

2. A Sub-task is added to the issue as shown in the screenshot below:-

![test2.png](https://community.atlassian.com/t5/image/serverpage/image-id/298205i5529920C3F36FA9A/image-size/large?v=v2&px=999 "test2.png")

3. Add some details for the Sub-task on the Create screen and click the Create button as shown in the screenshot below.

![test3.png](https://community.atlassian.com/t5/image/serverpage/image-id/298206i5576125DA396F692/image-size/large?v=v2&px=999 "test3.png")

4. Once the Sub-task is created, view it, and you will see that the comments, from the parent issue, have been copied as shown in the screenshot below:-

![test4.png](https://community.atlassian.com/t5/image/serverpage/image-id/298207i26EC9D1CD63BFA67/image-size/large?v=v2&px=999 "test4.png")

I hope this helps to solve your question. :-)  

Thank you and Kind regards,  
Ram


author: Manoj Gangwar
body: [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) , But Also, I want to exclude copy comments made by Automation user from parent issue to subtasks.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Manoj Gangwar](/t5/user/viewprofilepage/user-id/4484987)

In your last comment, you mentioned:-
> But Also, I want to exclude copy comments made by Automation user from parent issue to subtasks.

Have you included a filter in your Automation to ensure that the Comments will not be included in the Sub-task, or are you expecting Automation to include the comment in the Sub-task as well and ScriptRunner to exclude the same comment, i.e. from the comments copied from the Parent issue?

Please clarify.

Either way, if you want to exclude the Automation Comment from the Parent issue, you will need to update the code to:-  

```
if (issue.isSubTask()) {
    def parent = issue.parentObject

    def filter = parent.comments.findAll { it.body == 'This is an Automated Comment' } //The comment message set in Automation
    def output = parent.comments - filter

    issue.set {
        output.each {
            issue.addComment(it.body)
        }
    }
}
```

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Thank you and Kind regards,

Ram


author: Manoj Gangwar
body: [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015) ,

I am expecting ScriptRunner to exclude the same comment, i.e. from the comments copied from the Parent issue.


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Manoj Gangwar](/t5/user/viewprofilepage/user-id/4484987)

Thanks for the clarification.

If you only intend to exclude the Automation comment copied from the Parent, the solution I provided in my last comment will do this.

Here is the code once again:-

```
if (issue.isSubTask()) {
    def parent = issue.parentObject

    def filter = parent.comments.findAll { it.body == 'This is an Automated Comment' } //The comment message set in Automation
    def output = parent.comments - filter

    issue.set {
        output.each {
            issue.addComment(it.body)
        }
    }
}
```

Thank you and Kind regards,

Ram


author: Rebekka Heilmann _viadee_
body: Are you set on using a script rather than Automation?

comments: author: Mathew Lederman
body: [@Rebekka Heilmann _viadee_](/t5/user/viewprofilepage/user-id/5182395)

I'm working with [@Manoj Gangwar](/t5/user/viewprofilepage/user-id/4484987) on this. We would be prefer to use Automation rather than a post-function as this could be applied to the whole project rather than to each workflow. However, I think we would still need to use some scripting to exclude comments made by the automation user.


author: Rebekka Heilmann _viadee_
body: If you branch over the comments, you should be able to access the author with a smart value {{issue.comments.author.displayName}} *(.id should work as well)* and use that in a condition.


Tags: automation

data-center

jira-service-management

jira-service-management-server

listener

script

script-listener

scriptrunner

server