# Add comment with attachment JSM

**URL:** https://forum.library.adaptavist.com/t/add-comment-with-attachment-jsm/158
**Category:** Scripts
**Tags:** jira, post-function, cloud
**Created:** [June 7, 2022, 5:40pm UTC](https://forum.library.adaptavist.com/t/add-comment-with-attachment-jsm/158 "2022-06-07T17:40:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DanAlonso14](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.library.adaptavist.com/danalonso14/32/108_2.png) [@DanAlonso14](https://forum.library.adaptavist.com/u/DanAlonso14)
#### Post date: [June 7, 2022, 5:40pm UTC](https://forum.library.adaptavist.com/t/add-comment-with-attachment-jsm/158/1 "2022-06-07T17:40:54Z")

</div>

Hi,  
I want to add a comment visible on the Customer portal, and include one of the attachments on the issue.  
How can I do it? I planning to implement this on a Post-Function.

---

<div class="post-metadata">

### Author: ![imadero](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.library.adaptavist.com/imadero/32/59_2.png) [@imadero](https://forum.library.adaptavist.com/u/imadero)
#### Post date: [June 13, 2022, 5:58am UTC](https://forum.library.adaptavist.com/t/add-comment-with-attachment-jsm/158/2 "2022-06-13T05:58:48Z")

</div>

Hi @DanAlonso14 , i hope this can guide you on how to add the comment. [https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-comments/#api-rest-api-3-issue-issueidorkey-comment-post)

---

<div class="post-metadata">

### Author: ![DanAlonso14](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.library.adaptavist.com/danalonso14/32/108_2.png) [@DanAlonso14](https://forum.library.adaptavist.com/u/DanAlonso14)
#### Post date: [July 25, 2022, 6:46pm UTC](https://forum.library.adaptavist.com/t/add-comment-with-attachment-jsm/158/3 "2022-07-25T18:46:08Z")

</div>

Hi @imadero thank you for your answer. Yes I know how to add the comment, but how can I add an Attachment to that comment, that’s the real challenge. Any ideas?

---

<div class="post-metadata">

### Author: ![imadero](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.library.adaptavist.com/imadero/32/59_2.png) [@imadero](https://forum.library.adaptavist.com/u/imadero)
#### Post date: [August 3, 2022, 10:20am UTC](https://forum.library.adaptavist.com/t/add-comment-with-attachment-jsm/158/4 "2022-08-03T10:20:52Z")

</div>

I didn’t do it before, but maybe the solution is to add the attachment directly to the issue, and then in the comment body, reference this attachment. How i suppose it’s possible to do that:  
On the API that send you before, the body can follow this Object, **[Atlassian document format](https://developer.atlassian.com/cloud/jira/platform/apis/document/structure/)**. So probably in the comment body you can something similar to that:

```auto
"body": {
    "type": "doc",
    "version": 1,
    "content": [
      {
        "type": "paragraph",
        "content": [
          {
            "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.",
            "type": "text"
          }
        ]
      }, 
{
  "type": "mediaGroup",
  "content": [
    {
      "type": "media",
      "attrs": {
        "type": "file",
        "id": "6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5",
        "collection": "ae730abd-a389-46a7-90eb-c03e75a45bf6",
      }
    },
    {
      "type": "media",
      "attrs": {
        "type": "file",
        "id": "6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5",
        "collection": "ae730abd-a389-46a7-90eb-c03e75a45bf6",
      }
    }
  ]
}
    ]
  }
}

```

I’m not sure if you can combine different **Atlassian document formats** on the same body, but could be an option to fit your needs.  
The information about the media type is on [Node - mediaGroup](https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/mediaGroup/)

Let me know if it works for you.

---

<div class="post-metadata">

### Author: ![DanAlonso14](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.library.adaptavist.com/danalonso14/32/108_2.png) [@DanAlonso14](https://forum.library.adaptavist.com/u/DanAlonso14)
#### Post date: [October 25, 2022, 10:32pm UTC](https://forum.library.adaptavist.com/t/add-comment-with-attachment-jsm/158/5 "2022-10-25T22:32:15Z")

</div>

Thank you @imadero  
I was able to reproduce your example, but I cannot implement it until this bug is solved:  
[https://jira.atlassian.com/browse/JRACLOUD-72208](https://jira.atlassian.com/browse/JRACLOUD-72208)

This is how I implement it with ScriptRunner, it will work as long as the attachment is already added in some previous comment:

```auto
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

JsonNodeFactory jnf = JsonNodeFactory.instance;
ObjectNode payload = jnf.objectNode();
ObjectNode body = payload.putObject("body");
A:{
    body.put("type", "doc");
    body.put("version", 1);
    ArrayNode content = body.putArray("content");
    ObjectNode content0 = content.addObject();
    ObjectNode content1 = content.addObject();
    
    B:{
        content0.put("type", "paragraph");
        content = content0.putArray("content");
        content0 = content.addObject();
        C:{
            content0.put("text", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque eget venenatis elit. Duis eu justo eget augue iaculis fermentum. Sed semper quam laoreet nisi egestas at posuere augue semper.");
            content0.put("type", "text"); 
        }
        
        content1.put("type", "mediaGroup");
        content = content1.putArray("content");
        content1 = content.addObject();
        D:{
            content1.put("type", "media");
            ObjectNode attrs = content1.putObject("attrs");
            attrs.put("type", "file");
            attrs.put("id", "4d6a6bgg8f-b7f9-4b6b-b367-09e93369b234");
        }
       
    }
}

logger.info(body.toString())

def resultComment = post("/rest/api/3/issue/ABC-10775/comment").header("Content-Type", "application/json").body(payload).asObject(Map)

```

Thanks for your help.  
Daniel

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/adaptavistlibrary/original/1X/ceb18adf14fa58b4c6de03aa69ada8b07359620c.png) [@system](https://forum.library.adaptavist.com/u/system)
#### Post date: [April 23, 2023, 10:33pm UTC](https://forum.library.adaptavist.com/t/add-comment-with-attachment-jsm/158/6 "2023-04-23T22:33:08Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
