Scriptrunner - Listener on Create event get fields Edited

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

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

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. 

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

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.

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. 

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

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?

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

  

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? 

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 Join to answer
Community showcase
Emilee Spencer
Published Friday in Marketplace Apps

Marketplace Spotlight: DeepAffects

Hello Atlassian Community! My name is Emilee, and I’m a Product Marketing Manager for the Marketplace team. Starting with this post, I'm kicking off a monthly series of Spotlights to highlight Ma...

61 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot