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);
}
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")
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);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is the type of your custom field? Multi-line text field?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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}
"""
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.