Creating a custom script listener

Anandhi A February 16, 2014

Hi,

I want to update a custom field with date and time when an issue is being moved from one project to another.

I tried a code to just update custom field with date. I put it in Script Console and chose Groovy as engine and Ran it. I was able to update the custom field.

But i want to know how to put this into a custom listener for Issue Moved event?

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.ManagerFactory
import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.config.webwork.actions.*
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.*
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp;
import java.text.*
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.MultipleSettableCustomFieldType
import com.atlassian.jira.issue.customfields.impl.MultiSelectCFType
import com.atlassian.jira.issue.customfields.impl.MultiUserCFType
import com.atlassian.jira.issue.customfields.impl.SelectCFType
import com.atlassian.jira.issue.customfields.impl.TextCFType
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.util.BuildUtils
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.web.bean.PagerFilter
import com.onresolve.jira.groovy.canned.CannedScript
import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
import org.apache.log4j.Category
import com.atlassian.crowd.embedded.api.User
import java.util.Date

IssueFactory issueFactory = ComponentManager.getInstance().getIssueFactory()
IssueManager issueManager = ComponentManager.getInstance().getIssueManager()
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager()
CustomField cf= customFieldManager.getCustomFieldObject("customfield_10595")
final JiraAuthenticationContext authenticationContext = ComponentManager.getInstance().getJiraAuthenticationContext()
User currentUser = authenticationContext.getLoggedInUser()

MutableIssue issue = issueManager.getIssueObject("OKDAC-5259")
String str = "test"
String mydueDate= new SimpleDateFormat("dd/MMM/yyyy HH:mm:ss").format(Calendar.getInstance().getTime());
issue.setCustomFieldValue(cf,(java.lang.Object)mydueDate)
def cfval = issue.getCustomFieldValue(cf)
issueManager.updateIssue(issue.getAssignee(), issue, EventDispatchOption.DO_NOT_DISPATCH, false);
return "Issue Key is: ${issue.key} ... ${cfval}"


2 answers

1 accepted

0 votes
Answer accepted
Anandhi A February 17, 2014

Thanks Nic.

I created a class like the one below and put my code inside it. Saved it as SetDateField.groovy. Put the .groovy file inside WEB-INF/classes/com/custom. Created a custom listener and gave com.custom.SetDateField in the groovy class field and it is working now.

class SetDateField extends AbstractIssueEventListener 
{
    Category log = Category.getInstance(SetDateField.class) 
    SetDateField() {
    log.setLevel(org.apache.log4j.Level.DEBUG) 
} 

void issueMoved(IssueEvent event) {
    //My code
}

Thanks for your quick response.

Regards,
Anandhi

 

1 vote
Nic Brough -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.
February 16, 2014

You can't just cast a string to a date - the lines using "new simpleDateFormat" is completely wrong because myDueDate should be a date and not a string, and the following line is worse because it casts a string to an object rather than a date.

Try losing the cast in the second line, and making mydueDate a date instead of a string.

Anandhi A February 16, 2014

Thanks Nic. I made the change and compiled the code. I have placed the class file in JIRA_InstallationDir/atlassian-jira/WEB-INF/classes/com/custom. But i got the error

Problem loading class: IOException while opening groovy source: com.custom.SetDateField

What do i do to make this work?

Is there a different way to save and compile the file?

Nic Brough -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.
February 16, 2014

That usually means it can't read the file you've given it. Check location, permissions etc.

Anandhi A February 16, 2014

Oh. Can you please tell me how to save the code and compile it. I guess am doing it wrong. I used Eclipse.

Thanks.

Nic Brough -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.
February 16, 2014

Eh? Compile it? The whole point of the script runner is that the code is run as it is. What part of the docs said you should compile it?

Anandhi A February 17, 2014

I referred the following link.

https://answers.atlassian.com/questions/259127/how-to-add-custom-listener-groovy-class-for-script-runner-plugin

I have never tried any of this before. I saved my code in a file SetDateField.groovy and put it inside WEB-INF/classes/com/custom folder. Please tell me if this is correct or is it something else that i should do.

Nic Brough -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.
February 17, 2014

So you did not compile it? Why did you say you had?

Have you checked the permissions on the file?

Can you tell us *exactly* what you have done, step by step, clearly, with paths and file names and what you're putting in fields?

Suggest an answer

Log in or Sign up to answer