How to use Scriptrunner for confluence to copy comments from one page to another
Asked by Sujatha M:

There is a scenario where i need to copy comments from one page to another when i copy the page. Adaptavist suggested to use scriptrunner plugin for confluence. Can anybody suggest the script to copy the comments. Also i cannot find the event Pagecopy in the scriptrunner event triggers.


Answers:
author: Katy Kelly
body: Hi Sujatha,

You can use the following script in Script Console. You just need to change the id of the pages you are referring to.

[Here's](https://confluence.atlassian.com/confkb/how-to-get-confluence-page-id-648380445.html)how you can get the page id.

```
import com.atlassian.confluence.pages.Pageimport com.atlassian.confluence.pages.Commentimport com.atlassian.confluence.pages.PageManagerimport com.atlassian.sal.api.component.ComponentLocatorimport com.atlassian.confluence.core.DefaultSaveContextdef pageManager = ComponentLocator.getComponent(PageManager)Page initialPage = pageManager.getPage(98311)Page targetPage = pageManager.getPage(98305)for (Comment comment : initialPage.getComments()) {targetPage.addComment(comment)}pageManager.saveContentEntity(targetPage, DefaultSaveContext.MINOR_EDIT)
```

Regards,

Katy

Adaptavist Product Support

comments: author: Sujatha M
body: Hi Kelly,

This is a more generic requirement and not specific to a page. Every time i copy a page it should copy the comments.

I want to bind this to the pagecopy event actually, but i cannot find the event trigger to copy page event in the scriptrunner plugin, however i can find oher triggers like pagecreate, pagemove pageupdated etc.

Regards

Sujatha


author: Rafael Franco
body: Hi Sujatha,

If you use the ScriptEventHandlers you'll find the PageCopyEvent. With this event you can get the destination and origin.

Did you try this?

Regards,

Rafael


author: Sujatha M
body: Yes i have tried this and I could not find the PageCopy event. That is when i reached out.

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


author: Rafael Franco
body: That is very strange since it displays fine on mine as you can see:

![Screen Shot 2017-07-07 at 09.49.20.png](https://community.atlassian.com/t5/image/serverpage/image-id/2805i9075C63D793B68E9/image-size/large?v=v2&px=999 "Screen Shot 2017-07-07 at 09.49.20.png")Which version of ScriptRunner and Confluence you have installed?


author: Sujatha M
body: Confluence version 5.10.8 and scriptrunner version 5.0.8


author: Rafael Franco
body: Sujatha, the PageCopyEvent is only available starting from Confluence 6.x as it can be seen here:

<https://docs.atlassian.com/confluence/latest-server/com/atlassian/confluence/event/events/content/page/PageCopyEvent.html>

So unfortunately, it will not be available in your Confluence version, you would need to upgrade.


author: Sujatha M
body: Thanks Rafael. We will upgrade and check.

Also how do we get source and destination pages without giving IDS like below

**Page initialPage = pageManager.getPage(98311) Page targetPage = pageManager.getPage(98305)**

I would want to bind the copyevent with the script which will copy the comments which are open( not resolvd) from source to destination. In this case we will not be able to specify the ID of the source and destination page as it is not known.


author: Rafael Franco
body: The PageCopyEvent contains that, so:

<https://docs.atlassian.com/confluence/latest-server/com/atlassian/confluence/event/events/content/page/PageCopyEvent.html#getOrigin-->

<https://docs.atlassian.com/confluence/latest-server/com/atlassian/confluence/event/events/content/page/PageCopyEvent.html#getDestination-->


author: Sujatha M
body: Is this how it has to be used? It shows error.

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


author: Rafael Franco
body: ```
def event = event as PageCopyEventdef originalPage = event.getOrigin()def destinationPage = event.getDestination()
```


author: Sujatha M
body: Some event package has to be imported?

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


author: Rafael Franco
body: import com.atlassian.confluence.event.events.content.page.PageCopyEvent


author: Sujatha M
body: Now the script is fine. But the comments are not being copied.

Please check the attachment. I have attached the original and destination document also![original page.png](https://community.atlassian.com/t5/image/serverpage/image-id/2815i493EFCA4161BC6D8/image-size/large?v=v2&px=999 "original page.png")![destination pae.png](https://community.atlassian.com/t5/image/serverpage/image-id/2816iF962D83977AC85EC/image-size/large?v=v2&px=999 "destination pae.png").

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


author: Sujatha M
body: Any update on this? Any reason why the comments are not being copied from one page to another when the page is copied even though we have enabled the script.


author: Rafael Franco
body: Hi Sujatha,

I just tested and Confluence does not trigger the PageCopyEvent for a single page, it will only trigger if you include the child pages.

There is no way to make this work withou this option set.

![Screen Shot 2017-07-12 at 16.07.38.png](https://community.atlassian.com/t5/image/serverpage/image-id/3007i99319503488114F3/image-size/large?v=v2&px=999 "Screen Shot 2017-07-12 at 16.07.38.png")


author: Sujatha M
body: We tried this option by including the child pages while copying. But what happens is, the comments are copied to the new page. However the same has been removed from the parent page. This is not the expectation. The expectation is to copy and not to move the comments, it should be available in both the parent and the child pages.

Regards

Sujatha


author: Rafael Franco
body: What you need is to create a new comment based on the the source one and add than one instead.


author: Sujatha M
body: ;;This is the script

import com.atlassian.confluence.pages.Page  
import com.atlassian.confluence.pages.Comment  
import com.atlassian.confluence.pages.PageManager  
import com.atlassian.sal.api.component.ComponentLocator  
import com.atlassian.confluence.core.DefaultSaveContext  
import com.atlassian.confluence.event.events.content.page.PageCopyEvent

def pageManager = ComponentLocator.getComponent(PageManager)

def event = event as PageCopyEvent  
def originalPage = event.getOrigin()  
def destinationPage = event.getDestination()

for (Comment comment : originalPage.getComments()) {  
destinationPage.addComment(comment)   
}  
pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)


author: Rafael Franco
body: ```
import com.atlassian.confluence.core.SaveContext
import com.atlassian.confluence.pages.CommentManager
import com.atlassian.confluence.pages.Page
import com.atlassian.confluence.pages.Comment
import com.atlassian.confluence.pages.PageManager
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.confluence.core.DefaultSaveContext
import com.atlassian.confluence.event.events.content.page.PageCopyEvent
def pageManager = ComponentLocator.getComponent(PageManager)
def commentManager = ComponentLocator.getComponent(CommentManager)

def event = event as PageCopyEvent
def originalPage = event.getOrigin()
def destinationPage = event.getDestination()

public static final SaveContext NODATECHANGE_CONTEXT = new DefaultSaveContext(true, false, true)

for (Comment comment : originalPage.getComments()) {
    final Comment newComment = new Comment()
    newComment.bodyAsString = comment.getBodyAsString()
    newComment.creationDate = comment.getCreationDate()
    newComment.lastModificationDate = comment.getLastModificationDate()
    newComment.creator = comment.getCreator()

    commentManager.saveContentEntity(comment, NODATECHANGE_CONTEXT)

    destinationPage.addComment(newComment)
}

pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)
```


author: Sujatha M
body: ![Script_Error.png](https://community.atlassian.com/t5/image/serverpage/image-id/3655i30B47C6DCFFAF6CD/image-size/large?v=v2&px=999 "Script_Error.png")


author: Rafael Franco
body: Just remove the public static final


author: Sujatha M
body: ![Script_Error1.png](https://community.atlassian.com/t5/image/serverpage/image-id/3656i20894A2F35D0B155/image-size/large?v=v2&px=999 "Script_Error1.png")


author: Sujatha M
body: After removing the public static final, We are getting the above error. Also by the above solution will both inline and page comments be copied?


author: Sujatha M
body: Any update on this?


author: Katy Kelly
body: Hi Sujatha,

We cannot tell what your code is since your screenshot only shows half of it.

Also, it shows an error in Line 23. Did you correct that?


author: Sujatha M
body: #Below is the script after removing public static line

#error s thrown in the below line

#commentManager.saveContentEntity(comment, #NODATECHANGE_CONTEXT)

variable - NODATECHANGE_CONTEXT is undeclared

#############################################

import com.atlassian.confluence.core.SaveContext  
import com.atlassian.confluence.pages.CommentManager  
import com.atlassian.confluence.pages.Page  
import com.atlassian.confluence.pages.Comment  
import com.atlassian.confluence.pages.PageManager  
import com.atlassian.sal.api.component.ComponentLocator  
import com.atlassian.confluence.core.DefaultSaveContext  
import com.atlassian.confluence.event.events.content.page.PageCopyEvent  
def pageManager = ComponentLocator.getComponent(PageManager)  
def commentManager = ComponentLocator.getComponent(CommentManager)

def event = event as PageCopyEvent  
def originalPage = event.getOrigin()  
def destinationPage = event.getDestination()

for (Comment comment : originalPage.getComments()) {  
final Comment newComment = new Comment()  
newComment.bodyAsString = comment.getBodyAsString()  
newComment.creationDate = comment.getCreationDate()  
newComment.lastModificationDate = comment.getLastModificationDate()  
newComment.creator = comment.getCreator()

commentManager.saveContentEntity(comment, NODATECHANGE_CONTEXT)

destinationPage.addComment(newComment)  
}

pageManager.saveContentEntity(destinationPage, DefaultSaveContext.MINOR_EDIT)