Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to create an internal comment in servicedesk using scriptrunner?

Saarvaani Vadlamani February 27, 2017

I tried creating an internal comment using the sample code provided in documentation:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.json.JSONObject

final SD_PUBLIC_COMMENT = "sd.public.comment"

def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": true])]
commentManager.create(issue, user, "my internal comment", null, null, new Date(), properties, true)

But i end up getting the below error:

Cannot call com.atlassian.jira.util.json.JSONObject#<init> (java.util.Map <java.lang.String,java.lang.Object>) with arguments [java.util.LinkedHashMap <java.lang.string, java.lang.Boolean>]

Any help is appreciated.

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 28, 2017

My guess is that you're only getting this error from the type checker, as shown below, right?

Screen Shot 2017-02-28 at 4.15.54 PM.png

That's actually benign, and the script should still run okay. The issue is that the type checker isn't perfect, and may not understand that in Groovy you can pass a LinkedHashMap to a method requiring a Map and it will work just fine. You can workaround this limitation by providing type information if the error really bugs you, but that's purely optional.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.util.json.JSONObject
final SD_PUBLIC_COMMENT = "sd.public.comment"
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": true] as Map)]
commentManager.create(issue, user, "my internal comment", null, null, new Date(), properties, true)
Amro Hassaan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 19, 2017

Hi Jonny and Saarvaani,

I am using the same exact code but the comments are created external rather than internal and I don't know why. Here is the code i am using. It just tracks spesific projects and copies their comments as internal to a service desk project:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.util.json.JSONObject
import com.atlassian.jira.user.util.UserManager
import org.apache.log4j.Category

def iis = ComponentAccessor.getOSGiComponentInstanceOfType(IssueIndexingService.class)
def ilm = ComponentAccessor.getComponent(IssueLinkManager.class)
def userManager = ComponentAccessor.getOSGiComponentInstanceOfType(UserManager.class)
final SD_PUBLIC_COMMENT = "sd.public.comment"

def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
def event = event as IssueEvent
def issue = event.issue
def user = userManager.getUserByName("xxx@mycompany.org")
def cttReqs = ilm.getLinkCollection(issue, user)?.getOutwardIssues("Implements")?.findAll{it.projectObject.key.equalsIgnoreCase("ctt")}

if (cttReqs.size() > 0) {
def commentManager = ComponentAccessor.getCommentManager()
def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": true] as Map)]
cttReqs.each {
commentManager.create(it, user, "Comment From ${issue.key} "+event.comment.body, null, null, new Date(), properties, true)
}


}

Jonny Carter
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 21, 2017

Hey, amro. Out of curiosity, what versions of JIRA & JIRA Service Desk are you using?

Paul Bijlsma May 29, 2017

We are using the same code and can't manage to get an internal comment created either. JIRA v7.3.4 and JSD v3.4.1. Has anyone found a solution for this?

Paul Bijlsma May 29, 2017

This is our code (JIRA v7.3.4 and JSD v3.4.1):

// Required Imports
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.user.util.UserManager;
import com.atlassian.jira.util.json.JSONObject

final SD_PUBLIC_COMMENT = "sd.public.comment"

// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getUser().name
log.warn("CurrentUser: " + CurrentUser)

// Get the Manager classes required
def commentManager = ComponentAccessor.getCommentManager()


//get linked issue:
def linkedIssues = [] as List<Issue>;
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
Issue issue = event.issue
log.warn("issue: " + issue)


def counter = 0
issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
    if (issueLink.issueLinkType.id == 10260) { 
        def linkedIssue = issueLink.destinationObject
        counter = counter + 1
        log.warn("linkedIssue " + counter + ": " + linkedIssue)
        String CommentText = "test for fix version change in " + issue.key
        def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": true] as Map)]
        log.warn("properties: " + properties)

        commentManager.create(linkedIssue, CurrentUser, CommentText, null, null, new Date(), properties, true)

    }
}


  

This is the error:

2017-05-30 08:48:20,903 WARN [runner.ScriptRunnerImpl]: CurrentUser: jira-admin
2017-05-30 08:48:20,904 WARN [runner.ScriptRunnerImpl]: issue: COM-217
2017-05-30 08:48:20,907 WARN [runner.ScriptRunnerImpl]: linkedIssue 1: AC-28654
2017-05-30 08:48:20,908 WARN [runner.ScriptRunnerImpl]: properties: [sd.public.comment:{"internal":true}]
2017-05-30 08:48:20,918 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2017-05-30 08:48:20,919 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.comments.DefaultCommentManager.create() is applicable for argument types: (com.atlassian.jira.issue.IssueImpl, java.lang.String, java.lang.String, null, null, java.util.Date, java.util.LinkedHashMap, java.lang.Boolean) values: [AC-28654, jira-admin, test for fix version change in COM-217, ...]
Possible solutions: create(com.atlassian.jira.issue.Issue, java.lang.String, java.lang.String, java.lang.String, java.lang.Long, java.util.Date, boolean), grep(), create(com.atlassian.jira.issue.Issue, java.lang.String, java.lang.String, boolean), create(com.atlassian.jira.issue.Issue, java.lang.String, java.lang.String, java.lang.String, java.lang.Long, boolean), create(com.atlassian.jira.issue.Issue, com.atlassian.jira.user.ApplicationUser, java.lang.String, java.lang.String, java.lang.Long, java.util.Date, boolean), create(com.atlassian.jira.issue.Issue, com.atlassian.jira.user.ApplicationUser, java.lang.String, java.lang.String, java.lang.Long, java.util.Date, java.util.Map, boolean)
 at Script162$_run_closure1.doCall(Script162.groovy:39)
 at Script162.run(Script162.groovy:29)
Paul Bijlsma May 30, 2017

Never mind. Fixed it!

Looking in the wrong place. I was focused on the JSON object, but instead the wrong type was refering to the fact I was using the user name instead of the user object.

Here is the tidied up working code (also changed getUser to getLoggedInUser() so no longer deprecated for my version):

 

// Required Imports
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.util.json.JSONObject

final SD_PUBLIC_COMMENT = "sd.public.comment"

// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
log.warn("CurrentUser: " + CurrentUser)

// Get the Manager classes required
def CommentManager = ComponentAccessor.getCommentManager()


//get linked issue:
def linkedIssues = [] as List<Issue>;
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
Issue issue = event.issue
log.warn("issue: " + issue)


def counter = 0
issueLinkManager.getOutwardLinks(issue.id).each {issueLink ->
    if (issueLink.issueLinkType.id == 10260) { 
        def linkedIssue = issueLink.destinationObject
        counter = counter + 1
        log.warn("linkedIssue " + counter + ": " + linkedIssue)
        String CommentText = "test for fix version change in " + issue.key
        def properties = [(SD_PUBLIC_COMMENT): new JSONObject(["internal": true] as Map)] 
        log.warn("properties: " + properties)
        CommentManager.create(linkedIssue, CurrentUser, CommentText, null, null, new Date(), properties, true)
       

    }
}

 

Shyam Goda May 15, 2018

Hi Paul,

I used your updated script to insert internal comment in our Jira Service Desk, however, it creates only one row in the "entity_property" table corresponding to the "jiraaction" table row, with the below listed field-values:

entity_name = 'CommentProperty',
property_key = 'sd.public.comment',
json_value = {"internal": true}

 

With this single row, the comment still appears as public comment on the issue and not as an internal comment.

 

For the comment to appear as internal comment, it needs another row in "entity-property" table for the same "jiraaction" row with the following values:

entity_name = 'sd.public.comment',
property_key = 'sd.public.comment',
json_value = {"internal": true}

 

Since the CommentManager.create() method takes only a single Map of String:JSON pair, I am not sure how to get the second row inserted there.

 

Any help of alternative way is welcome. We have an urgent need for this solution.

Thanks in advance.

 

Regards,

Shyam

TAGS
AUG Leaders

Atlassian Community Events