Custom Listener not getting invoked

Hi,

Initial requirement was when an issue is moved from source project to destination project need to copy value from Custom Field "Solution" in Source Project to "Comment" in destination project.

I wrote Groovy script for this "AddSolutionAsCommentListener.groovy" and placed this at following location: /home/jiranew/atlassian-jira-6.0.8-standalone/atlassian-jira/WEB-INF/classes/com/groovylistener/AddSolutionAsCommentListener.groovy

Then I added this class as Custom Listener on "Issue Moved" event for Source project -> then tried moving an issue from source project to destination project but couldn't find anything relevant in logs.

After that I added this class as Custom Listener on "Issue Created" event for Destination project -> then tried moving an issue from source project to destination project still couldn't find anything relevant in logs. Please help me.

Here is the code of Custom Listener:

package com.groovylistener

import org.apache.log4j.Category
import com.atlassian.crowd.embedded.api.User
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.comments.CommentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
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

class AddSolutionAsCommentListenerTest extends AbstractIssueEventListener {

   Category log = Category.getInstance(AddSolutionAsCommentListenerTest.class)

   ComponentManager compManager = ComponentManager.getInstance()
   CustomFieldManager customFieldManager = compManager.getCustomFieldManager()

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

   IssueManager issueManager = compManager.getIssueManager()
 
   public AddSolutionAsCommentListenerTest()
   {
       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
 
     CommentManager commentMgr = compManager.getCommentManager()
     commentMgr = (CommentManager) compManager.getComponentInstanceOfType(CommentManager.class)

     sourceFieldName = "Solution"

     CustomField customFieldSrc = customFieldManager.getCustomFieldObjectByName(sourceFieldName)

     def results = getSearchResults(request)
   
     results.getIssues().eachWithIndex { iss , i ->
        issue = issueManager.getIssueObject(iss.id)
 
        log.info "Processing ${issue.key}"
        i += 1
        String reporterUser = issue.getReporter();
        def sourceFieldVal = "Solution -"  + issue.getCustomFieldValue(customFieldSrc)
     
        log.info "Actual Value of Solution: ${sourceFieldVal}"
 
        log.info "Creating Comment"
        commentMgr.create(issue, reporterUser, sourceFieldVal, false)
 
        log.info "Added Comment"
       }
    }

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

1 answer

Hi @Zahabiya Barbhaiwala,

  1. Just to be sure, add an log.error in your constructor, this will be shown if the cosntructor is called
  2. Remove your custom code and just log the event incomming, to make sure you don't have problems with your code
  3. Does the script work if you run it via the console?

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,241 views 14 19
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
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