Hi community,
I am not an expert in scripting but am really eager to learn more and to get things done by using groovy and Script Runner. Today I kindly ask you for your support since I get stuck. I have tried a lot and I have read a lot of articles in Script Runner documentation + library as well as around here. Most of the articles generally lead into the right direction but I can't put all these things together in order to achieve the following:
- based on a choice from a single select field (mandatory upon issue creation)
- an existing Service Desk organisation shall be set
- an existing particular reporter for this organisation shall be set
- once the issue has been created.
We're using Jira (Server) 7.13.11 and Service Desk 3.6.11 plus Script Runner for Jira 6.2.1. To get this task done I was going to set up a script to be used as a script listener via Script Runner only on the "Issue Created" event.
Please find below the current work on my script:
(parts that are commented out were part of my testing)
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManager
import groovy.transform.Field
import org.apache.log4j.Logger
import org.apache.log4j.Level
@Field Logger log = Logger.getLogger("Partner/Organisation setter:")
log.setLevel(Level.DEBUG)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
UserManager userManager = ComponentAccessor.getUserManager()
IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
MutableIssue updatedIssue = (MutableIssue) event.getIssue()
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("")
//def issueReporter
//def partner = customFieldManager.getCustomFieldObject("customfield_10111")
//def organisation = customFieldManager.getCustomFieldObject("customfield_10103")
def partner = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10111");
//config.partner = issue.getCustomFieldValue(partner);
def organisation = ComponentAccessor.customFieldManager.getCustomFieldObject("customfield_10103");
//config.organisation = issue.getCustomFieldValue(organisation);
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//CustomField partner = customFieldManager.getCustomFieldObjectByName("partner")
//CustomField organisation = customFieldManager.getCustomFieldObjectByName("organisation")
// String organisationValue
// def partner = updatedIssue.getCustomFieldValue(partner)
log.info("Inspecting: " + updatedIssue.key)
log.info("Partner: " + partner)
// ### end of initial part ###
// ### start of test part ###
if(partner == "Test Partner"){
log.info("Updating Partner: Test Partner")
def issueReporter = userManager.getUserByKey('testpartner@domain.de')
issue.setReporter(issueReporter)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
organisation.updateValue(null, updatedIssue, new ModifiedValue(updatedIssue.getCustomFieldValue(organisation), "Test Partner"),new DefaultIssueChangeHolder())
//updatedIssue.setCustomFieldValue(organisation)
issueManager.updateIssue(event.getUser(), (MutableIssue)updatedIssue, EventDispatchOption.ISSUE_UPDATED, false)
}
And this is the current outcome in log:
2020-08-14 10:34:49,868 INFO [Partner/Organisation setter:]: Inspecting: CS-96814
2020-08-14 10:34:49,868 INFO [Partner/Organisation setter:]: Partner: Partner
2020-08-14 10:34:49,872 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-08-14 10:34:49,872 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.IllegalArgumentException: Can only compare Field objects.
at com.atlassian.jira.issue.fields.ImmutableCustomField.compareTo(ImmutableCustomField.java:1841)
at Script642.run(Script642.groovy:58)
Many thanks in advance for for help!
Best regards,
Johannes