Skip to main content
Example scripts
arrow icon
To homepage
Jira Service Desk
Data centre icon
Data Center

Create a Comment on a Jira Service Desk Ticket in Groovy

Created 1 year ago, Updated 1 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira Service Desk (3.10 - 4.6)
compatibility bullet
ScriptRunner For Jira (5.6.14)
Language |
groovy
import com.atlassian.jira.bc.issue.comment.CommentService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.servicedesk.api.comment.ServiceDeskCommentService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin

@WithPlugin("com.atlassian.servicedesk")

final String issueKey = "TEST-1" // the key of the issue
final String comment = "This is a comment" // the comment you want to add

static void createServiceDeskComment(MutableIssue issueToComment, String text, Boolean internal) {
    def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser

    if (issueToComment.projectObject.projectTypeKey.key == "service_desk") {
        def serviceDeskCommentService = ComponentAccessor.getOSGiComponentInstanceOfType(ServiceDeskCommentService)

        def createCommentParameters = serviceDeskCommentService.newCreateBuilder()
            .author(user)
            .body(text)
            .issue(issueToComment)
            .publicComment(!internal)
            .build()

        serviceDeskCommentService.createServiceDeskComment(user, createCommentParameters)
    } else {
        def commentParameters = CommentService.CommentParameters.builder()
            .author(user)
            .body(text)
            .issue(issueToComment)
            .build()

        def commentService = ComponentAccessor.getComponent(CommentService)
        def validationResult = commentService.validateCommentCreate(user, commentParameters)
        commentService.create(user, validationResult, true)
    }
}

//Example of function in use
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueByCurrentKey(issueKey)

createServiceDeskComment(issue, comment, true) //false means public, true means internal
Having an issue with this script?
Report it here