Hello,
I would like to add a comment with text added to the custom field after an issue is updated. I created a Listener and wrote this script https://www.screencast.com/t/leYKRSibGr8z
The problem is: I cant get the custom field value. I tried hard, but I cant manage it. I'm using the same approach in Behaviour script and it is working well. I mean this line
def cFieldValue = underlyingIssue.getCustomFieldValue(cField).toString()
Can someone help me to get the value of the custom field?
The whole script is here:
log.warn("issue update Listener was fired")
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().name
//log.warn("Show User:" + CurrentUser)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def CommentManager commentManager = ComponentAccessor.getCommentManager()
def cField = customFieldManager.getCustomFieldObjectByName("DB Custom 1")
//log.warn("Show cField" + cField)
def cFieldValue = underlyingIssue.getCustomFieldValue(cField).toString()
def cFieldValue =??
Thanks in advance
Thank you Mark!
The whisperer helped me when created this post, I think it is very similar solution to yours.
The final code here: https://www.screencast.com/t/xXNlBOhe
L.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Lubomir Styskala could you please post the code here, unfortunately due to security reasons I am unable to open the page.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Berenberg.io Team What script do you mean? The code from the screen above is also in the comment below (from Jul 18 2018)
def issue = event.issue as Issue
def cFieldValue = issue.getCustomFieldValue(cField)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have another problem in the next step of the script, I cant insert a comment, following message appears:
"cannot find matching method...."
log.warn("issue update Listener was fired");
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
def issue = event.issue as Issue
// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser().name
//log.warn("Show User:" + CurrentUser)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.commentManager
def cField = customFieldManager.getCustomFieldObjectByName("DB Custom 1")
def cFieldValue = issue.getCustomFieldValue(cField)
log.warn("Show cFieldValue" + cFieldValue)
if(cFieldValue){
// Add a comment to the Issue
commentManager.create(issue, CurrentUser, cFieldValue, false)
log.warn("Show cFieldValue" + cFieldValue)
}
Problem line is "commentManager.create(issue, CurrentUser, cFieldValue, false)"
code screen here: https://www.screencast.com/t/C3abtlVLzC
Comment is inserted according to this tutorial: https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/postfunctions/append-generate-comment.html
I think everything is done well, but it isn't working. Thank you for your further help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Lubomir Styskala
Here is the problem that variable currentUser contains String object with name. And for create it must me ApplicationUser object.
It should be like this
log.warn("issue update Listener was fired");
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
def issue = event.issue as Issue
// Get a pointer to the current logged in user
def CurrentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//log.warn("Show User:" + CurrentUser.name)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def commentManager = ComponentAccessor.commentManager
def cField = customFieldManager.getCustomFieldObjectByName("DB Custom 1")
def cFieldValue = issue.getCustomFieldValue(cField).toString()
log.warn("Show cFieldValue" + cFieldValue)
if(cFieldValue){
// Add a comment to the Issue
commentManager.create(issue, CurrentUser, cFieldValue, false)
log.warn("Show cFieldValue" + cFieldValue)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Mark Markov
I added:
import com.atlassian.jira.user.ApplicationUser;
I changed (ApplicationUser to the beginning and deleted.name() at the end ):
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Still getting the same error when trying to create a comment.
Code screenshot: https://www.screencast.com/t/Yqnv0G7U5o
Do you have any further idea?
Thanks in advance!
L.
edit: ok, I missed your edit with code, will try it and let you know.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can ignore this error :) It happen because getCustomFieldValue returns Object value and compiler doesnt know what exactly there.
If it annoys you, you can get value like this
def cFieldValue = issue.getCustomFieldValue(cField).toString()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Mark,
you are totally right. It is working. I wonder how did you find the solution? But anyway, thank you so far :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Remove the quotes from around cField.
You are passing in cField as a string, which won't work. cField is supposed to be a custom field object, as defined in Mark's code in the "def cField = ..." line.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.