Script listener commentBody to customField

Aileen March 8, 2018

Hello,

I am trying to get the comment body of all comments by a particular user to be put into a custom field, but it's not working. (Using ScriptRunner by the way, and JIRA Software server 7.3.5)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption

def issue = event.issue as Issue
MutableIssue issueToUpdate = (MutableIssue) issue;
def issueManager = ComponentAccessor.getIssueManager();

def commentManager = ComponentAccessor.getCommentManager()
def user = event.getComment().getAuthorApplicationUser()
def commentBody = commentManager.getLastComment(issue).getBody()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def myfield = customFieldManager.getCustomFieldObjectByName("custom field name")

if (user == "username"){
issueToUpdate.setCustomFieldValue(myfield, commentBody);
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
}

 

1 answer

1 accepted

1 vote
Answer accepted
Ivan Tovbin
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.
March 8, 2018

Hi Aileen,

I think this is your problem:

if (user == "username")

Looking at your code, 'user' variable is an ApplicationUser object. And you are trying to compare an object to a string which cannot be done hence your 'if' statement condition failing and all the code in its closure not being executed.

The correct way to do this is like so:

if (user.getUsername() == "username")
Aileen March 8, 2018

Thank you ! That worked.

I also need to not overwrite the current value though, and I can't seem to get the custom field value as a string... I've tried a few ways, but maybe you know at a glance?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption

def issue = event.issue as Issue
MutableIssue issueToUpdate = (MutableIssue) issue;
def issueManager = ComponentAccessor.getIssueManager();

def commentManager = ComponentAccessor.getCommentManager()
def user = event.getComment().getAuthorApplicationUser()
def commentBody = commentManager.getLastComment(issue).getBody()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def myfield = customFieldManager.getCustomFieldObjectByName("custom field")
def customfieldValue = (String)issue.getCustomFieldValue(myfield)

if (user.getUsername() == "username"){
    issueToUpdate.setCustomFieldValue(myfield, "${customfieldValue} + ${commentBody}");
    issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
}
Ivan Tovbin
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.
March 8, 2018

What is the type of your custom field? Multi-line text field?

Aileen March 8, 2018

Yeah - Update, no, sorry it was a hideable free text field. I'll try with a multi-line fiekd (can just hide it with behaviors anyway).

 

Ivan Tovbin
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.
March 8, 2018

Well then I'd try to construct a multi-line string first and then pass it as the new field value, like so:

String newValue = """
${issue.getCustomFieldValue(myfield)
${commentBody}
"""
Aileen March 9, 2018

I think it's just not getting the custom field value as a string. I tried your suggestion, but still getting these NullPointerExceptions:

2018-03-09 10:35:05,266 ajp-nio-127.0.0.1-8009-exec-6 ERROR username 635x13455x2 5xltl1 192.168.2.146 /rest/api/2/issue/JIRA-401/comment [c.o.scriptrunner.runner.AbstractScriptListener] Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException
        at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
        at com.atlassian.jira.issue.Issue$getCustomFieldValue$6.call(Unknown Source)
        at Script191.run(Script191.groovy:15)
Ivan Tovbin
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.
March 9, 2018

The error indicates that there's something wrong with the field you are trying the value of. Check if you've used the correct field name (or even better use the id instead) when declaring your custom field.

Aileen March 9, 2018

Awesome ! Thanks @Ivan Tovbin :) I could have sworn I tried that before, but apparently not because it worked now.

Final product (in case anyone is curious):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
//Get required Interfaces
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.getCommentManager()
//Define issue (event issue)
def issue = event.issue as Issue
MutableIssue issueToUpdate = (MutableIssue) issue;
//Get user
def user = event.getComment().getAuthorApplicationUser()
//Get comment, comment body and define new custom field values.
def specialcomment = commentManager.getLastComment(issue)
def commentBody = specialcomment.getBody()
def myfield = customFieldManager.getCustomFieldObject('customfield_12880')
def customfieldValue = issue.getCustomFieldValue(myfield)
String newValue = """${issue.getCustomFieldValue(myfield)}
${commentBody} """

//If git user then copy comment to cf and then delete comment.
if (user.getUsername() == "username"){
    issueToUpdate.setCustomFieldValue(myfield, newValue)
    commentManager.delete(specialcomment)
    issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false)
}

Suggest an answer

Log in or Sign up to answer