ScriptRunner get custom field value - Custom Listener Script

Lubomir Styskala
Contributor
July 18, 2018

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

3 answers

1 accepted

8 votes
Answer accepted
Mark Markov
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.
July 18, 2018
Hello
Use
event.issue.getCustomFieldValue(cField)
Istead of underlyingIssue.
underlyingIssue object exist only in behaviours.
In listeners we have event object and issue object in postfunctions, validators and conditions.
Lubomir Styskala
Contributor
July 18, 2018

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.

Like # people like this
Berenberg.io Team September 1, 2020

@Lubomir Styskala  could you please post the code here, unfortunately due to security reasons I am unable to open the page.

Lubomir Styskala
Contributor
September 1, 2020

@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)
Like Srikanth Ganipisetty likes this
1 vote
Lubomir Styskala
Contributor
July 18, 2018

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!

0 votes
Mark Markov
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.
July 19, 2018

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)

}
Lubomir Styskala
Contributor
July 19, 2018

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.

Mark Markov
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.
July 19, 2018

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()

 

Lubomir Styskala
Contributor
July 19, 2018

Mark,

you are totally right. It is working. I wonder how did you find the solution? But anyway, thank you so far :) 

Daniel H. Fisher March 27, 2024
issue.getCustomFieldValue("cField").toString() yields "field not found" error. Any suggestons?
Nic Brough -Adaptavist-
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 28, 2024

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.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events