Scriptrunner - Listener on Create event get fields

Jon Starbird
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.
November 16, 2017

I am working on collecting Fields/Values on issue creation and while I can get the fields/values the problem is I'm getting fields that are not even for this issue type and are not even available to the user to use when creating.

Is there some way to get the Field/Values that are ONLY part of the Issue that the event was triggered for?

Another issue I'm having is that despite checking for null or empty I still get items that are empty. <solved most of these>

Here is the test code I've got working based on other examples found online:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;


def cfm = ComponentAccessor.getCustomFieldManager();


def fieldsUsed = cfm.getCustomFieldObjects(event.issue);

fieldsUsed.each
{
def fval = event.issue.getCustomFieldValue(it).toString().trim();
if (event.issue.getCustomFieldValue(it) != null && !fval.isEmpty())
{
log.warn("Field: ${it} - Value: ${event.issue.getCustomFieldValue(it)}");
}
}

 

1 answer

1 accepted

2 votes
Answer accepted
Thanos Batagiannis _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.
November 17, 2017

Hi Jon,

So the following script will return to you all the field ids of issues that are in the Create Issue Screen for the issue that triggered the listener.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.operation.IssueOperations

def issue = event.issue

def screenItems = ComponentAccessor.getFieldScreenRendererFactory().getFieldScreenRenderer(issue, IssueOperations.CREATE_ISSUE_OPERATION).getAllScreenRenderItems()
screenItems?.eachWithIndex { it, index->
log.debug "$index field id is ${it.fieldScreenLayoutItem.fieldId}"
}

And if you know which fields were [resent and their ids, then you can find the value ... 

Regarding the issue of getting back fields that are empty, I would suggest in you condition to take advantage of the Groovy's truth and has in you fi statement something like 

if (issue.getCustomFieldValue(it)){ 
log.warn "Field: $it - Value: ${issue.getCustomFieldValue(it)}");
}

Hope that helps,

Thanos

Jon Starbird
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.
November 17, 2017

Thanks for that info.

Regarding the condition statement, I had tried that but I still got back empty or fields with Null in them. 

Even with my current method:

 def fval = event.issue.getCustomFieldValue(it).toString().trim();
if (event.issue.getCustomFieldValue(it) != null && !fval.isEmpty())

I still get blank values, but it is less than if I didn't do the IsEmpty check. 

Thanos Batagiannis _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.
November 17, 2017

Hey Jon, 

Can you try again with just

def fval = event.issue.getCustomFieldValue(it)
if (fval) {
log.debug "It has value $fval"
}

You do not really need the 

.toString().trim() 
Jon Starbird
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.
November 17, 2017

Same results. I still need to add in the other code you gave me to get only the fields from the issue itself so let me see if that eliminates more of them.

Some/most seem to be not part of the issue I'm testing on.

Jon Starbird
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.
November 17, 2017

Your code is working for me, thanks. I know I can grab the field name in one way but does the fieldScreenLayoutItem objectg have fieldnames? I can't seem to find anything online that indicates it does. 

Thanos Batagiannis _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.
November 20, 2017

Hi Jon, 

So the 

def screenItems = ComponentAccessor.getFieldScreenRendererFactory().getFieldScreenRenderer(issue, IssueOperations.CREATE_ISSUE_OPERATION).getAllScreenRenderItems()

will return to you a List<FieldScreenRenderLayoutItem

Hope that helps, Thanos

Jon Starbird
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.
November 20, 2017

Thanks for that info.

I have another question, not sure if this is a bug or a non-issue.

When I'm working on the Custom Listener for Create events and I go in and make a script change, then go and create an issue the Logs with the scriptrunner listener item doesn't show the correct data.

An event will get registered but it is not for the issue I just created but rather for a previous one that was already logged.

I tried using the Scriptrunner build in script to clear caches but that did not fix the issue. 

My concern is, is the Custom Listener is not properly picking up the data when a Create event gets fired off?

David Puchosic November 29, 2017

Did you sort out your problem?  I'm having a similar scenario firing a Custom Listener on IssueClosed events. It acts like the issue gets cached, so as I transition say Issue AA-200, then go to another issue AA-205 and transition it, the event code logs AA-200's data.  JIRA v7.2.2 & ScriptRunner v5.2.2

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager

// get current issue
def String issueKey = event.issue
IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue currentIssue = issueManager.getIssueObject(issueKey)
log.debug "IssueKey: " + currentIssue.key

  

Jon Starbird
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.
November 29, 2017

I did not. I hadn't checked it in a real scenario yet so I don't know if this is just the Listener log info that shows in the UI or if it genuinely gets the wrong data. 

Have you verified if it is really doing things incorrectly rather than just the log in the Scriptrunner UI? 

David Puchosic December 4, 2017

Good question, it's acting better in my last round of tests. I can't exactly determine the fix :( -  I was using separate browser sessions for editing and examining the results of the Scripted Listener, but wouldn't think that would matter. I'm going to proceed putting the full code in place and monitor. Thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events