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

This widget could not be displayed.

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 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...

207 views 3 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