Custom Date Picker is Empty when Updated via Listener

Kevin Comco June 7, 2021

Use Case: I want to create a custom field that will be updated when a user 'flags' an issue as blocked.

Attempted Solution: I've created a Custom Date Picker (and Date Time Picker) as well as a Listener that looks for updates to the Flagged field.  If the issue is flagged, I populate the Picker with the current date/time via a groovy script.

Observations: I was finally able to get the Picker to display on the issue selected screen and it shows the correct values.  However, when attempting to query on the Custom Picker field, it shows as EMPTY.  If I manually choose the Picker and set a date, the JQL works as expected.  e.g. Flagged is not EMPTY and "Flagged Date" is not EMPTY returns nothing unless I set Flagged Date via the GUI.

I did reindex and no change.

Thoughts?

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp

//Set up the issue
def issueEventManager = ComponentAccessor.getIssueEventManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issue = event.issue as Issue

//The field to WATCH and its VALUE
def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find{it.name == "Flagged"}
def chgField = customFieldManager.getCustomFieldObjects(event.issue).find{it.name == "Flagged"}
def chgFieldValue = chgField.getValue(event.issue).toString()

//The field we want to change
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find{it.name == "Flagged Date"}
def changeHolder = new DefaultIssueChangeHolder()

//Set up the current date
def newDate = java.sql.Timestamp.valueOf("2021-06-07 00:00:00.0")

if(!change)
{
//we're done
}
else
{
change.each
{
if(chgFieldValue=="null")
{
//Flag was cleared - clear out date
tgtField.updateValue(null,issue.new ModifiedValue(issue.getcustomFieldValue(tgtField), null),changeHolder)
}
else
{
//Flag was set - set current date
tgtField.updateValue(null,issue.new ModifiedValue(issue.getcustomFieldValue(tgtField), newDate),changeHolder)
}
}
}

 

UPDATE:  I think the issue must be something in the POST (or whatever it's called in JIRA) of the updates.  The automatic date entry via the script above LOOKS fine on the screens and the values appear and are cleared as expected.  However, if I try to query on the date field using JQL, the date field comes back as EMPTY.  Here's the weird part.  If I add a COMMENT or some other update to the issue (via the GUI, not within the script - tried that), then the date field is queryable.  I'm not sure what's going on.

2 answers

1 accepted

0 votes
Answer accepted
Kevin Comco June 8, 2021

It turns out that I needed to INDEX the issue (not the whole project) once I'd completed the updates so that the custom field would be searchable via JQL.  I found this question that clued me in to the ultimate answer: https://community.atlassian.com/t5/Jira-questions/Custom-field-value-not-updated-in-the-Index/qaq-p/988429

 

Here's what I had to add above (in the various appropriate places):

import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
.
.
.
def issueIndexService = ComponentAccessor.getComponent(IssueIndexingService.class)
boolean wasIndexing = ImportUtils.isIndexIssues()
.
.
.
ImportUtils.setIndexIssues(true)
issueIndexService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)

 

That only took a week to figure out. :)  Cheers! 

1 vote
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 8, 2021

Hi @Kevin Comco

I would assume that for your Listener, you are using both IssueLinkCreatedEvent as well as the IssueLinkDeltedEvent

Below is a sample working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import java.sql.Timestamp

def issue = event.issueLink.sourceObject as MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def sampleDateTime = customFieldManager.getCustomFieldObjectsByName("Sample Date Time")[0]

def allLinks = issueLinkManager.getOutwardLinks(issue.id)
def changeHolder = new DefaultIssueChangeHolder()
def now = new Timestamp(new Date().time)

if(allLinks.size()>0) {
allLinks.findAll {
def sourceObject = it.sourceObject as MutableIssue
if (it.issueLinkType.name == "Blocks") {
sampleDateTime.updateValue(null, sourceObject, new ModifiedValue(sourceObject.getCustomFieldValue(sampleDateTime), now), changeHolder)
}
}
} else {
sampleDateTime.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(sampleDateTime), null), changeHolder)
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the Listener configuration in my environment:-

listener_config.png

The image above displays an error message saying: No such property: issueLink for class: java.lang.Object. You can ignore this.

Below is a basic test:-

1) When the ticket is first created, the custom field Sample Date Time is empty because it has no value, as shown in the image below:-

image1.png

2) Now, when an issue is linked as a blocker, the Sample Date Time is updated to the current timestamp as shown in the images below:-

image2.png

image3.png

image4.png

3) Now, when the Blocker is removed, the Sample Date Time field is removed as shown in the print screens below:-

image5.png

image6.pngimage7.png

I hope this helps to answer your question. :)


Thank you and Kind Regards,

Ram

Kevin Comco June 8, 2021

@Ram Kumar Aravindakshan _Adaptavist_ 

What I'm trying to do is use the included Flagged custom field (right click in issue list and 'add flag') to trigger the date field update.  I'm not keying it on any linkage to other issues via blocked by/ blocks, etc...  And so I've set it up to listen to 'IssueUpdated' events rather than the IssueLinkCreated and IssueLinkDeleted as you mentioned.  As a result, several of the methods/properties you reference to not be found (e.g. issueLink and issueLinkManager). 

I have been able to get the basic behavior you show in your tests - the date fills and clears appropriately.  But when I try to query it via JQL in the find issues pane, it doesn't show.  For example: Flagged = "Impediment" and "Flagged Date" is not EMPTY returns nothing... UNLESS I manually set the date via the date picker in the GUI.  Then the query finds it fine.  It has to be something in how the values are being set.  I'm looking at another question that mentions setFormValue that I'm experimenting with to see if that's what I've missed.

 

Thanks!

Suggest an answer

Log in or Sign up to answer