Problem with custom Listener

Hi, I develop this Custom Listener:

package com.meredith.jira.groovy

import org.apache.log4j.Category

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.listeners.reindex.ReindexMessageListener
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter

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


class PutNoMI extends AbstractIssueEventListener {
  Category log = Category.getInstance(PutNoMI.class)

  //private static final Long FILTER_ID = 10201
  ComponentManager cm = ComponentManager.getInstance()

  CustomFieldManager cfm = cm.getCustomFieldManager()

  def optionsManager = ComponentAccessor.getOptionsManager()

  def authenticationContext = cm.getJiraAuthenticationContext()
  JiraServiceContext ctx = new JiraServiceContextImpl(authenticationContext.getUser())
  def searchProvider = cm.getSearchProvider()
  SearchRequestService searchRequestService = cm.getSearchRequestService()
  SearchRequest request = searchRequestService.getFilter(ctx,10201)

  IssueManager issueManager = cm.getIssueManager()

  public PutNoMI()
  {
    log.setLevel(org.apache.log4j.Level.DEBUG)
    log.debug "debug statements"
    log.info "in constructor"
  }
  @Override
  void workflowEvent(IssueEvent event) {
    MutableIssue issue = event.issue as MutableIssue

    CustomField workPriority = cfm.getCustomFieldObjects(issue).find {it.name == "Comment MI"}

    //MODIFICAT
    def fieldConfig = workPriority.getRelevantConfig(issue)
    def newValue = optionsManager.getOptions(fieldConfig)?.find{it.value == "No"}

    //log.info "Value to change CommentMI in next issue: ${newValue}"


    def results = getSearchResults(request)
    results.getIssues().eachWithIndex { iss , i ->
      issue = issueManager.getIssueObject(iss.id)

      log.info "Processing ${issue.key}"
      i += 1
      def commentMI = issue.getCustomFieldValue(workPriority)
      log.info "Actual Value of CommentMI: ${commentMI}"

      log.info "GO, changin commentMI..."
      issue.setCustomFieldValue(workPriority, newValue)

      def newCurrentIssuePriority = issue.getCustomFieldValue(workPriority)
      log.info "CHANGED! New value of commentMI in this issue: ${newCurrentIssuePriority}"
    }

  }

  SearchResults getSearchResults(SearchRequest sr) {
    return searchProvider.search(sr.getQuery(), authenticationContext.getUser(), PagerFilter.getUnlimitedFilter())
  }

}

But I have problem, my logs shows that the custom Field is changed but when I enter to issue, the custom field 'Comment MI' still the same..

These are my logs:

2013-04-09 12:35:50,449 http-bio-8080-exec-7 ERROR admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [groovy.canned.utils.ConditionUtils] javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method contains() on null object
2013-04-09 12:35:50,479 http-bio-8080-exec-7 ERROR admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [groovy.canned.utils.ConditionUtils] javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method contains() on null object
2013-04-09 12:35:50,486 http-bio-8080-exec-7 DEBUG admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] debug statements
2013-04-09 12:35:50,486 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] in constructor
2013-04-09 12:35:50,534 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] Processing PI-1067
2013-04-09 12:35:50,535 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] Actual Value of CommentMI: Yes
2013-04-09 12:35:50,535 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] GO, changin commentMI...
2013-04-09 12:35:50,535 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] CHANGED! New value of commentMI in this issue: No
2013-04-09 12:35:50,567 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] Processing PI-1066
2013-04-09 12:35:50,569 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] Actual Value of CommentMI: Yes
2013-04-09 12:35:50,569 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] GO, changin commentMI...
2013-04-09 12:35:50,569 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] CHANGED! New value of commentMI in this issue: No
2013-04-09 12:35:50,571 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] Processing PI-1062
2013-04-09 12:35:50,574 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] Actual Value of CommentMI: Yes
2013-04-09 12:35:50,574 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] GO, changin commentMI...
2013-04-09 12:35:50,574 http-bio-8080-exec-7 INFO admin 755x2398x1 ucrbou 192.94.163.146 /rest/api/2/issue/PI-1067/comment [meredith.jira.groovy.PutNoMI] CHANGED! New value of commentMI in this issue: No
2013-04-09 12:35:50,995 com.atlassian.mywork.client.reliability.DefaultUnreliableWorker:thread-1 WARN admin 629x57x1 boasfh 192.94.163.146 /secure/admin/util/JellyRunner.jspa [apache.commons.httpclient.HttpMethodBase] Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
2013-04-09 12:35:51,825 com.atlassian.mywork.client.reliability.DefaultUnreliableWorker:thread-1 WARN support 629x57x1 boasfh 192.94.163.146 /secure/admin/util/JellyRunner.jspa [apache.commons.httpclient.HttpMethodBase] Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

Any idea of my problem?


3 answers

1 accepted

Accepted Answer
1 vote

Thanks for answer guys!

I share my code for anyone have the same problem, and this is solution or you want to copy to antoher implementation :

package com.meredith.jira.groovy

import org.apache.log4j.Category

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.web.bean.PagerFilter

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

import com.atlassian.jira.workflow.WorkflowUtil
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.event.type.EventDispatchOption

class PutNoMI extends AbstractIssueEventListener {
  Category log = Category.getInstance(PutNoMI.class)

  //private static final Long FILTER_ID = 10201
  ComponentManager cm = ComponentManager.getInstance()

  def userManager = cm.getUserUtil()
  def userId = userManager.getUser("admin")

CustomFieldManager cfm = cm.getCustomFieldManager()

  def optionsManager = ComponentAccessor.getOptionsManager()

  def authenticationContext = cm.getJiraAuthenticationContext()
  JiraServiceContext ctx = new JiraServiceContextImpl(authenticationContext.getUser())
  def searchProvider = cm.getSearchProvider()
  SearchRequestService searchRequestService = cm.getSearchRequestService()
  SearchRequest request = searchRequestService.getFilter(ctx,10201)

  IssueManager issueManager = cm.getIssueManager()

  public PutNoMI()
  {
    log.setLevel(org.apache.log4j.Level.DEBUG)
    log.debug "debug statements"
    log.info "in constructor"
  }
  @Override
  void workflowEvent(IssueEvent event) {
    MutableIssue issue = event.issue as MutableIssue

    CustomField workPriority = cfm.getCustomFieldObjects(issue).find {it.name == "Comment MI"}

    //MODIFICAT
    def fieldConfig = workPriority.getRelevantConfig(issue)
    def newValue = optionsManager.getOptions(fieldConfig)?.find{it.value == "No"}

//log.info "Value to change CommentMI in next issue: ${newValue}"


    def results = getSearchResults(request)
    results.getIssues().eachWithIndex { iss , i ->
      issue = issueManager.getIssueObject(iss.id)

      log.info "Processing ${issue.key}"
      i += 1

      def commentMI = issue.getCustomFieldValue(workPriority)
      log.info "Actual Value of CommentMI: ${commentMI}"

      log.info "GO, changin commentMI..."
      issue.setCustomFieldValue(workPriority, newValue)

      def newCurrentIssuePriority = issue.getCustomFieldValue(workPriority)
      log.info "CHANGED! New value of commentMI in this issue: ${newCurrentIssuePriority}"

      issueManager.updateIssue(userId, issue, EventDispatchOption.ISSUE_UPDATED, false)
    }

  }

  SearchResults getSearchResults(SearchRequest sr) {
    return searchProvider.search(sr.getQuery(), authenticationContext.getUser(), PagerFilter.getUnlimitedFilter())
  }

}

If you are not setting the field value through workflow for current ticket, then you will have to Update the issue once you have set some field's value in a request.

issueManager.updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, true);

Thanks! Works for me :)

1 vote

You need to do more than issue.setCustomFieldValue()... look into using the IssueService.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Sep 25, 2018 in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

449 views 7 5
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