CustomField needs to be updated twice before it appears in search results

Hi,

I am using below groovy script to update a custom field of type DateTime Picker every time another field is changed. Update goes fine and I can see that the value of the customfield has been updated. But when I perform a jql search, like say "AutomationUpdatedOn > startOfDay()", it doesn't show me these updated fields unless I update the same field twice. I have tried many things like re-indexing, reIndex(issue) inside my code, but not help.

class UpdateField extends AbstractIssueEventListener {
    Logger log = Logger.getLogger(UpdateField.class)
    @Override
    void workflowEvent(IssueEvent event) {
        log.setLevel(org.apache.log4j.Level.DEBUG)

        def field = event.getChangeLog().getRelated('ChildChangeItem').any{
                it.field.toString().equals("Test Automation")
        }
        if(field){
            MutableIssue issue = event.issue
            ComponentManager componentManager = ComponentManager.getInstance()
            IssueIndexManager indexManager = ComponentManager.getIndexManager();
            CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
            def issueManager = componentManager.getIssueManager()
            CustomField automationUpdated = customFieldManager.getCustomFieldObjectByName( "AutomationUpdatedOn" );
            issue.setCustomFieldValue(automationUpdated,new Timestamp(new Date().getTime()))
            User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
            issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
            issue.store()
        }
    }
}

2 answers

1 accepted

This widget could not be displayed.
Ok, guys. I have the solution to this issue. Posting here if someone else might be interested.
Issue was with indexing and flushing the cache. Got it running after adding below code.



   if(field){
            MutableIssue issue = event.issue
 boolean wasIndexing = ImportUtils.isIndexIssues(); ImportUtils.setIndexIssues(true);             ComponentManager componentManager = ComponentManager.getInstance()
            CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
            def issueManager = componentManager.getIssueManager()
 IssueIndexManager indexManager = componentManager.getIndexManager()
            CustomField automationUpdated = customFieldManager.getCustomFieldObjectByName( "AutomationUpdatedOn" );
            issue.setCustomFieldValue(automationUpdated,new Timestamp(new Date().getTime()))
            issue.store()
            User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
            issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
 if((BuildUtils.getCurrentBuildNumber() as Long) < 614){ ManagerFactory.getCacheManager().flush(com.atlassian.jira.issue.cache.CacheManager.ISSUE_CACHE, issue) } indexManager.reIndex(issue); ImportUtils.setIndexIssues(wasIndexing); 

Why are you checking the build number?

This widget could not be displayed.

Anybody, guys?? Any hint, direction... any hope :)

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

158 views 2 0
Join discussion

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