Hi,
I'm writing my first script in script runner and running into some issues - hoping someone might be able to point me in the right direction.
I want to write a basic script that fires when an issue is assigned. Based on the JIRA group the user is a member of I want to update a custom field (select list) to set a value.
customfield_11300 = this is the custom field I want to set.
I hacked the below together using a few examples but cant seem to get it to work - any ideas?
import com.atlassian.jira.component.ComponentAccessor; import com.atlassian.jira.issue.CustomFieldManager; import com.atlassian.jira.issue.fields.CustomField; import com.atlassian.jira.issue.IssueManager; import com.atlassian.jira.issue.Issue; import com.atlassian.jira.user.util.UserManager; import com.atlassian.jira.event.type.EventDispatchOption; import com.atlassian.jira.issue.MutableIssue def groupManager = ComponentAccessor.getGroupManager() def customFieldManager = ComponentAccessor.getCustomFieldManager(); def cField = customFieldManager.getCustomFieldObject("customfield_11300"); def assignee = issue.getAssignee() if (assignee != null && groupManager.getUsersInGroup("APAC Team").contains(assignee)) { issue.setCustomFieldValue(cField,"APAC Team"); } else if (assignee != null && groupManager.getUsersInGroup("NA Team").contains(assignee)) { issue.setCustomFieldValue(cField,"NA Team"); } else if (assignee != null && groupManager.getUsersInGroup("EMEA Team").contains(assignee)) { issue.setCustomFieldValue(cField,"EU Team"); } else { issue.setCustomFieldValue(cField,"NA"); }
I'll assume that you are running this in a custom script listener.
What you are doing is setting the value of a temporary issue object.
You will need to store that issue object back to the database so that subsequent retrieval will show the new value and index it so that you can search based on that field.
What may also be missing is that what you are doing is attempting to set a text value to the field. Is that custom field a text field? If it's an Single Select field, then that won't work. When you set the value of a Single Select field, you need to supply an Option value, not a text value.
There are dozens of answers for both of these cases in the community.
Look for issueManager.updateIssue examples for the first bit and optionsManager.getOptions for the second.
The Adaptavist Script Library is also a good resource: https://library.adaptavist.com/entity/update-a-single-select-drop-down-custom-field
Hi Peter,
I appreciate your help on this so far. Yea that's correct, its a single select text field. I plan to use it as a listener to fire when an issue is assigned to someone (setting the "Regional Team" custom field based on what JIRA group they are in).
I managed to get the custom field updating correctly but as you pointed out the commit to the DB isn't working so I cant query on it.
I found the following link: https://community.atlassian.com/t5/Jira-questions/ScriptRunner-update-summary-from-another-issue/qaq-p/807688, which looks relevant but I'm trying to understand how to apply it to my code below.
Any further pointers you might have would be useful as I'm a little stuck on how to incorporate into my current code.
For now I'm just testing against a single issue "SUP-33800" but will also look to update to retrieve the current key (i guess using issue.getKey()
Thanks!
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue
// the name of the custom field (single select list type)
final String customFieldName = "Regional Team"
// the value of the new option to set
final String CSEMEA = "EMEA CS Team"
final String CSNA = "NA CS Team"
final String CSAPAC = "APAC CS Team"
final String NONCS = "Other Teams"
// the issue key to update
final String issueKey = "SUP-33800"
// the user making this update/commit
final String commituser = "JIRA Integration"
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey)
def customField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
def assignee = issue.getAssignee()
def groupManager = ComponentAccessor.getGroupManager()
def availableOptions = ComponentAccessor.optionsManager.getOptions(customField.getRelevantConfig(issue))
if (assignee != null && groupManager.getUsersInGroup("APAC Team").contains(assignee)) {
def optionToSet = availableOptions.find { it.value == CSAPAC }
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
}
else if (assignee != null && groupManager.getUsersInGroup("NA Team").contains(assignee)) {
def optionToSet = availableOptions.find { it.value == CSNA }
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
}
else if (assignee != null && groupManager.getUsersInGroup("EMEA Team").contains(assignee)) {
def optionToSet = availableOptions.find { it.value == CSEMEA }
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
}
else {
def optionToSet = availableOptions.find { it.value == NONCS }
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
When you use scriptrunner, you are bypassing a lot of built-in functionality. In this case, you will find that the Issue History is not updated with this change (audit trail) and the jira index is also not updated.
If you just want to re-index, you can call this simple method.
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
def reIndexIssue(issue){
//Re-index the issue after update
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexingService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
If the change history is important to you, then I'd recommend using the following methods instead of customField.updateValue
import com.atlassian.jira.event.type.EventDispatchOption
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
issue.setCustomFieldValue(customField, optionToSet)
issueManager.updateIssue( currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
reIndexIssue(issue)
Also, while reviewing your code, I thought I might offer some groovy options. I'm no authority, but I like to take advantage of those groovy shortcuts.
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
// the name of the custom field (single select list type)
final String customFieldName = "Regional Team"
// the value of the new option to set
final String CSEMEA = "EMEA CS Team"
final String CSNA = "NA CS Team"
final String CSAPAC = "APAC CS Team"
final String NONCS = "Other Teams"
def groupOptionMap = [
'APAC Team':CSAPAC,
'NA Team':CSNA,
'EMEA Team':CSEMEA,
]
// the issue key to update
final String issueKey = "SUP-33800"
// the user making this update/commit
final String commituser = "JIRA Integration"
//get some jira components
def issueManger = ComponentAccessor.issueManager.up
def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager
def groupManager = ComponentAccessor.groupManager
//objects to interact with
def issue = issueManager.getIssueByCurrentKey(issueKey)
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
def assignee = issue.assignee
def availableOptions = optionsManager.getOptions(customField.getRelevantConfig(issue))
if(assignee){
def group = groupManager.getGroupsForUser(assignee).find{ it.name in groupOptionMap.keySet()} ?: NONCS
def optionToSet = availableOptions.find{ it.value == optionMap[group] }
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
reIndexIssue(issue)
}
def reIndexIssue(issue){
//Re-index the issue after update
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexingService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
And finally, in an event listener ... you have access to the"event" object. And if the event is an issue event, you can just get the issue object associated with the event like this (not need to get the key and then get an object from the key):
def issue = event.issue
Hope this helps.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks so much for this Peter - truly appreciated - this is certainly looking a lot cleaner now!
I have updated the script you provided with my latest custom field values, I added the event listener and removed the line where i was getting an object from the key.
I'm now failing with this error - its complaining about the RequestCachingIssueManager - any ideas?
2019-08-21 15:25:03,546 ERROR [utils.ConditionUtils]: ************************************************************************************* 2019-08-21 15:25:03,546 ERROR [utils.ConditionUtils]: Condition failed on issue SUP-30428, built-in script:com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen groovy.lang.MissingPropertyException: No such property: up for class: com.atlassian.jira.issue.managers.RequestCachingIssueManager at Script183.run(Script183.groovy:31) 2019-08-21 15:25:03,546 ERROR [utils.ConditionUtils]: Script follows: import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.ModifiedValue import com.atlassian.jira.issue.util.DefaultIssueChangeHolder import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.index.IssueIndexingService import com.atlassian.jira.util.ImportUtils // the name of the custom field (single select list type) final String customFieldName = "Regional Team" // the value of the new option to set final String CSEMEA = "EMEA Client Services Team" final String CSNA = "NA Client Services Team" final String CSAPAC = "APAC Client Services Team" final String NONCS = "Non Client Services" def groupOptionMap = [ 'APAC Client Services Team':CSAPAC, 'NA Client Services Team':CSNA, 'EMEA Client Services Team':CSEMEA, ] // the issue key to update def issue = event.issue // the user making this update/commit final String commituser = "JIRA Integration" //get some jira components def issueManger = ComponentAccessor.issueManager.up def optionsManager = ComponentAccessor.optionsManager def customFieldManager = ComponentAccessor.customFieldManager def groupManager = ComponentAccessor.groupManager //objects to interact with def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName) def assignee = issue.assignee def availableOptions = optionsManager.getOptions(customField.getRelevantConfig(issue)) if(assignee){ def group = groupManager.getGroupsForUser(assignee).find{ it.name in groupOptionMap.keySet()} ?: NONCS def optionToSet = availableOptions.find{ it.value == groupOptionMap[group] } customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder()) reIndexIssue(issue) } def reIndexIssue(issue){ //Re-index the issue after update boolean wasIndexing = ImportUtils.isIndexIssues() ImportUtils.setIndexIssues(true) issueIndexingService.reIndex(issue) ImportUtils.setIndexIssues(wasIndexing) }
Code below:
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
// the name of the custom field (single select list type)
final String customFieldName = "Regional Team"
// the value of the new option to set
final String CSEMEA = "EMEA Client Services Team"
final String CSNA = "NA Client Services Team"
final String CSAPAC = "APAC Client Services Team"
final String NONCS = "Non Client Services"
def groupOptionMap = [
'APAC Client Services Team':CSAPAC,
'NA Client Services Team':CSNA,
'EMEA Client Services Team':CSEMEA,
]
// the issue key to update
def issue = event.issue
// the user making this update/commit
final String commituser = "JIRA Integration"
//get some jira components
def issueManger = ComponentAccessor.issueManager.up
def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager
def groupManager = ComponentAccessor.groupManager
//objects to interact with
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
def assignee = issue.assignee
def availableOptions = optionsManager.getOptions(customField.getRelevantConfig(issue))
if(assignee){
def group = groupManager.getGroupsForUser(assignee).find{ it.name in groupOptionMap.keySet()} ?: NONCS
def optionToSet = availableOptions.find{ it.value == groupOptionMap[group] }
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
reIndexIssue(issue)
}
def reIndexIssue(issue){
//Re-index the issue after update
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexingService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
Screenshots from editor:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Remove .up from line 31 ... that's a typo. I must have accidentally typed there when I was unaware of where my cursor was.
And for line 52, you can substitute the following
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
Or split it up and keep your existing line but add:
def issueIndexingService =ComponentAccessor.getComponent(IssueIndexingService.class)
That was a copy/paste mistake.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
thanks - a little closer. I kept line 52 as is and declared the below as suggested (see line 35) but now receive an error regarding it not being declared
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
I then also tried just substituting what i had on line 52 with - but that also threw an error
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You first attempt won't work because when you do a def in your main script, you can't use that variable inside a method. You have to either declare you variable without using def (the static type compiler will complain, but the variable it will be available globally) or declare it inside the method.
With the second method, the "Static type checking" errors don't always mean the code won't run. It's just that the type checker can't be sure. You can generally ignore those.
I use this little code snippet everywhere.
If you really want to clear those errors up, then you'll have to import all the different object classes you might use and declare them explicitly like
Issue issue = event.issue
/and
def reIndexIssue(Issue issue){...}
instead of
def issue = event.issue
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
OK so no errors in the editor - but its not working.
I upped the debug and can see the below errors - any idea what these mean?
2019-08-21 17:42:35,600 DEBUG [support.DefaultListableBeanFactory]: Creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen' 2019-08-21 17:42:35,600 DEBUG [groovyrunner.spring]: BeforeInstantiation [bean=com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen, type=com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen] 2019-08-21 17:42:35,600 DEBUG [groovyrunner.spring]: AfterInitialisation [bean=com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen, type=com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen] 2019-08-21 17:42:35,600 DEBUG [support.DefaultListableBeanFactory]: Finished creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen' 2019-08-21 17:42:35,600 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,600 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,601 ERROR [utils.ConditionUtils]: Debug message 2019-08-21 17:42:35,604 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,605 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,605 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,606 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,606 DEBUG [sql.AbstractSQLQuery]: select WORKLOG.id, WORKLOG.issueid, WORKLOG.author, WORKLOG.grouplevel, WORKLOG.rolelevel, WORKLOG.worklogbody, WORKLOG.created, WORKLOG.updateauthor, WORKLOG.updated, WORKLOG.startdate, WORKLOG.timeworked, PROJECT_ROLE.id, PROJECT_ROLE.name, PROJECT_ROLE.description from public.worklog WORKLOG left join public.projectrole PROJECT_ROLE on PROJECT_ROLE.id = WORKLOG.rolelevel where WORKLOG.issueid = ? order by WORKLOG.created asc 2019-08-21 17:42:35,607 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,610 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,610 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,611 DEBUG [sql.AbstractSQLQuery]: select "AO_56464C_APPROVAL"."ID", "AO_56464C_APPROVAL"."ID", "AO_56464C_APPROVAL"."ISSUE_ID", "AO_56464C_APPROVAL"."NAME", "AO_56464C_APPROVAL"."APPROVE_CONDITION_TYPE", "AO_56464C_APPROVAL"."APPROVE_CONDITION_VALUE", "AO_56464C_APPROVAL"."CREATED_DATE", "AO_56464C_APPROVAL"."COMPLETED_DATE", "AO_56464C_APPROVAL"."DECISION", "AO_56464C_APPROVAL"."STATUS_ID", "AO_56464C_APPROVAL"."SYSTEM_DECIDED", "AO_56464C_APPROVER"."APPROVAL_ID", "AO_56464C_APPROVER"."ID", "AO_56464C_APPROVER"."TYPE", "AO_56464C_APPROVER"."VALUE", "AO_56464C_APPROVERDECISION"."APPROVAL_ID", "AO_56464C_APPROVERDECISION"."DECISION", "AO_56464C_APPROVERDECISION"."ID", "AO_56464C_APPROVERDECISION"."SENT_DATE", "AO_56464C_APPROVERDECISION"."TO_STATUS_ID", "AO_56464C_APPROVERDECISION"."USER_KEY" from "public"."AO_56464C_APPROVAL" "AO_56464C_APPROVAL" left join "public"."AO_56464C_APPROVER" "AO_56464C_APPROVER" on "AO_56464C_APPROVAL"."ID" = "AO_56464C_APPROVER"."APPROVAL_ID" left join "public"."AO_56464C_APPROVERDECISION" "AO_56464C_APPROVERDECISION" on "AO_56464C_APPROVAL"."ID" = "AO_56464C_APPROVERDECISION"."APPROVAL_ID" where "AO_56464C_APPROVAL"."ISSUE_ID" = ? 2019-08-21 17:42:35,612 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,614 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,615 DEBUG [sql.AbstractSQLQuery]: select WORKLOG.id, WORKLOG.issueid, WORKLOG.author, WORKLOG.grouplevel, WORKLOG.rolelevel, WORKLOG.worklogbody, WORKLOG.created, WORKLOG.updateauthor, WORKLOG.updated, WORKLOG.startdate, WORKLOG.timeworked, PROJECT_ROLE.id, PROJECT_ROLE.name, PROJECT_ROLE.description from public.worklog WORKLOG left join public.projectrole PROJECT_ROLE on PROJECT_ROLE.id = WORKLOG.rolelevel where WORKLOG.issueid = ? order by WORKLOG.created asc 2019-08-21 17:42:35,616 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,616 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,616 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,616 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,617 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,617 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,617 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,618 DEBUG [sql.AbstractSQLQuery]: select "AO_54307E_ORGANIZATION"."ID", "AO_54307E_ORGANIZATION"."NAME" from "public"."AO_54307E_ORGANIZATION" "AO_54307E_ORGANIZATION" where "AO_54307E_ORGANIZATION"."ID" = ? 2019-08-21 17:42:35,618 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,619 DEBUG [sql.AbstractSQLQuery]: select "AO_575BF5_DEV_SUMMARY"."ID", "AO_575BF5_DEV_SUMMARY"."ISSUE_ID", "AO_575BF5_DEV_SUMMARY"."CREATED", "AO_575BF5_DEV_SUMMARY"."UPDATED", "AO_575BF5_DEV_SUMMARY"."PROVIDER_SOURCE_ID", "AO_575BF5_DEV_SUMMARY"."JSON" from "AO_575BF5_DEV_SUMMARY" "AO_575BF5_DEV_SUMMARY" where "AO_575BF5_DEV_SUMMARY"."ISSUE_ID" = ? 2019-08-21 17:42:35,620 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,620 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,621 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,621 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,622 DEBUG [jotm.jta]: Current.getStatus() 2019-08-21 17:42:35,625 DEBUG [utils.ConditionUtils]: Condition did not return a boolean, coercing to false 2019-08-21 17:42:35,625 DEBUG [support.DefaultListableBeanFactory]: Creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen' 2019-08-21 17:42:35,625 DEBUG [groovyrunner.spring]: BeforeInstantiation [bean=com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen, type=com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen] 2019-08-21 17:42:35,625 DEBUG [groovyrunner.spring]: AfterInitialisation [bean=com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen, type=com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen] 2019-08-21 17:42:35,625 DEBUG [support.DefaultListableBeanFactory]: Finished creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.FireEventWhen'
Is it related to the reindex method at the end? Possibly expecting a True/False response and not receiving that?
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
log.error "Debug message"
// the name of the custom field (single select list type)
final String customFieldName = "Regional Team"
// the value of the new option to set
final String CSEMEA = "EMEA Client Services Team"
final String CSNA = "NA Client Services Team"
final String CSAPAC = "APAC Client Services Team"
final String NONCS = "Non Client Services"
def groupOptionMap = [
'APAC Client Services Team':CSAPAC,
'NA Client Services Team':CSNA,
'EMEA Client Services Team':CSEMEA,
]
// the issue key to update
Issue issue = event.issue
//get some jira components
def issueManger = ComponentAccessor.issueManager
def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager
def groupManager = ComponentAccessor.groupManager
//objects to interact with
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
def assignee = issue.assignee
def availableOptions = optionsManager.getOptions(customField.getRelevantConfig(issue))
if(assignee){
def group = groupManager.getGroupsForUser(assignee).find{ it.name in groupOptionMap.keySet()} ?: NONCS
def optionToSet = availableOptions.find{ it.value == groupOptionMap[group] }
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
reIndexIssue(issue)
}
def reIndexIssue(Issue issue){
//Re-index the issue after update
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah ... I see this logline:
DEBUG [utils.ConditionUtils]: Condition did not return a boolean, coercing to false
This means that you've placed your code in a "Condition" inline script box.
This is usually only used to determine when to run an action or not. It's used as part of several different kinds of built-in listeners with other fields to setup.
Maybe you can share some screenshots of that overall configuration and make sure you include which listener configuration you selected.
The script should work as-is in a custom listener where there is no separate condition and action, jut what you decide to do in the script.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hmm thats odd - I added it as a listener to be triggered when the issue is assigned. I can see it being triggered correctly, that's clear as i see the attempt to execute through the history section (where I'm taking the log snippets i provided you from)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah, this is the wrong configuration to use.
This "Fires an event when condition is true" means pretty much what is says:
What you want is a custom listener:
Just add the script from above into the "inline script" and add your project and event.
This means:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Peter - I still cant get this to work for some reason. Output below - not sure if you have a view on what the issue is?
2019-08-23 01:15:20,625 DEBUG [support.DefaultListableBeanFactory]: Creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener' 2019-08-23 01:15:20,625 DEBUG [groovyrunner.spring]: BeforeInstantiation [bean=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener, type=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener] 2019-08-23 01:15:20,625 DEBUG [groovyrunner.spring]: AfterInitialisation [bean=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener, type=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener] 2019-08-23 01:15:20,625 DEBUG [support.DefaultListableBeanFactory]: Finished creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener' 2019-08-23 01:15:20,625 ERROR [runner.AbstractScriptRunner]: Debug message 2019-08-23 01:15:20,627 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,627 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,628 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,628 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,629 DEBUG [sql.AbstractSQLQuery]: select WORKLOG.id, WORKLOG.issueid, WORKLOG.author, WORKLOG.grouplevel, WORKLOG.rolelevel, WORKLOG.worklogbody, WORKLOG.created, WORKLOG.updateauthor, WORKLOG.updated, WORKLOG.startdate, WORKLOG.timeworked, PROJECT_ROLE.id, PROJECT_ROLE.name, PROJECT_ROLE.description from public.worklog WORKLOG left join public.projectrole PROJECT_ROLE on PROJECT_ROLE.id = WORKLOG.rolelevel where WORKLOG.issueid = ? order by WORKLOG.created asc 2019-08-23 01:15:20,629 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,630 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,630 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,630 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,632 DEBUG [sql.AbstractSQLQuery]: select "AO_54307E_ORGANIZATION"."ID", "AO_54307E_ORGANIZATION"."NAME" from "public"."AO_54307E_ORGANIZATION" "AO_54307E_ORGANIZATION" where "AO_54307E_ORGANIZATION"."ID" = ? 2019-08-23 01:15:20,632 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,633 DEBUG [sql.AbstractSQLQuery]: select "AO_56464C_APPROVAL"."ID", "AO_56464C_APPROVAL"."ID", "AO_56464C_APPROVAL"."ISSUE_ID", "AO_56464C_APPROVAL"."NAME", "AO_56464C_APPROVAL"."APPROVE_CONDITION_TYPE", "AO_56464C_APPROVAL"."APPROVE_CONDITION_VALUE", "AO_56464C_APPROVAL"."CREATED_DATE", "AO_56464C_APPROVAL"."COMPLETED_DATE", "AO_56464C_APPROVAL"."DECISION", "AO_56464C_APPROVAL"."STATUS_ID", "AO_56464C_APPROVAL"."SYSTEM_DECIDED", "AO_56464C_APPROVER"."APPROVAL_ID", "AO_56464C_APPROVER"."ID", "AO_56464C_APPROVER"."TYPE", "AO_56464C_APPROVER"."VALUE", "AO_56464C_APPROVERDECISION"."APPROVAL_ID", "AO_56464C_APPROVERDECISION"."DECISION", "AO_56464C_APPROVERDECISION"."ID", "AO_56464C_APPROVERDECISION"."SENT_DATE", "AO_56464C_APPROVERDECISION"."TO_STATUS_ID", "AO_56464C_APPROVERDECISION"."USER_KEY" from "public"."AO_56464C_APPROVAL" "AO_56464C_APPROVAL" left join "public"."AO_56464C_APPROVER" "AO_56464C_APPROVER" on "AO_56464C_APPROVAL"."ID" = "AO_56464C_APPROVER"."APPROVAL_ID" left join "public"."AO_56464C_APPROVERDECISION" "AO_56464C_APPROVERDECISION" on "AO_56464C_APPROVAL"."ID" = "AO_56464C_APPROVERDECISION"."APPROVAL_ID" where "AO_56464C_APPROVAL"."ISSUE_ID" = ? 2019-08-23 01:15:20,635 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,636 DEBUG [sql.AbstractSQLQuery]: select WORKLOG.id, WORKLOG.issueid, WORKLOG.author, WORKLOG.grouplevel, WORKLOG.rolelevel, WORKLOG.worklogbody, WORKLOG.created, WORKLOG.updateauthor, WORKLOG.updated, WORKLOG.startdate, WORKLOG.timeworked, PROJECT_ROLE.id, PROJECT_ROLE.name, PROJECT_ROLE.description from public.worklog WORKLOG left join public.projectrole PROJECT_ROLE on PROJECT_ROLE.id = WORKLOG.rolelevel where WORKLOG.issueid = ? order by WORKLOG.created asc 2019-08-23 01:15:20,636 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,637 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,637 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,637 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,638 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,638 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,638 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,641 DEBUG [sql.AbstractSQLQuery]: select "AO_575BF5_DEV_SUMMARY"."ID", "AO_575BF5_DEV_SUMMARY"."ISSUE_ID", "AO_575BF5_DEV_SUMMARY"."CREATED", "AO_575BF5_DEV_SUMMARY"."UPDATED", "AO_575BF5_DEV_SUMMARY"."PROVIDER_SOURCE_ID", "AO_575BF5_DEV_SUMMARY"."JSON" from "AO_575BF5_DEV_SUMMARY" "AO_575BF5_DEV_SUMMARY" where "AO_575BF5_DEV_SUMMARY"."ISSUE_ID" = ? 2019-08-23 01:15:20,642 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,642 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,643 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,643 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,644 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:15:20,648 DEBUG [support.DefaultListableBeanFactory]: Creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener' 2019-08-23 01:15:20,648 DEBUG [groovyrunner.spring]: BeforeInstantiation [bean=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener, type=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener] 2019-08-23 01:15:20,648 DEBUG [groovyrunner.spring]: AfterInitialisation [bean=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener, type=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener] 2019-08-23 01:15:20,648 DEBUG [support.DefaultListableBeanFactory]: Finished creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener'
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
log.error "Debug message"
// the name of the custom field (single select list type)
final String customFieldName = "Team Region"
// the value of the new option to set
final String CSEMEA = "EMEA Client Services Team"
final String CSNA = "NA Client Services Team"
final String CSAPAC = "APAC Client Services Team"
final String NONCS = "Non Client Services"
def groupOptionMap = [
'APAC Client Services Team':CSAPAC,
'NA Client Services Team':CSNA,
'EMEA Client Services Team':CSEMEA,
]
// the issue key to update
Issue issue = event.issue
//get some jira components
def issueManger = ComponentAccessor.issueManager
def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager
def groupManager = ComponentAccessor.groupManager
//objects to interact with
def customField = customFieldManager.getCustomFieldObjects(issue).findByName(customFieldName)
def assignee = issue.assignee
def availableOptions = optionsManager.getOptions(customField.getRelevantConfig(issue))
if(assignee){
def group = groupManager.getGroupsForUser(assignee).find{ it.name in groupOptionMap.keySet()} ?: NONCS
def optionToSet = availableOptions.find{ it.value == groupOptionMap[group] }
customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionToSet), new DefaultIssueChangeHolder())
reIndexIssue(issue)
}
def reIndexIssue(Issue issue){
//Re-index the issue after update
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
ComponentAccessor.getComponent(IssueIndexingService.class).reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Seeing that your "Debug Message" didn't appear anywhere in your log, it would seem that the "Issue Assigned" event was not fired when made your change.
You can try to listen for "All issue events" and see which event might fire your code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Still no luck.
It looks like the script is being executed based on the execution count increasing (see yellow highlight below)
Updated to fire on all issue events:
Error:
Time (on server): Thu Aug 22 2019 21:39:31 GMT-0400 (Eastern Daylight Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2019-08-23 01:39:31,360 DEBUG [support.DefaultListableBeanFactory]: Creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener' 2019-08-23 01:39:31,360 DEBUG [groovyrunner.spring]: BeforeInstantiation [bean=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener, type=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener] 2019-08-23 01:39:31,360 DEBUG [groovyrunner.spring]: AfterInitialisation [bean=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener, type=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener] 2019-08-23 01:39:31,360 DEBUG [support.DefaultListableBeanFactory]: Finished creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener' 2019-08-23 01:39:31,361 ERROR [runner.AbstractScriptRunner]: Debug message 2019-08-23 01:39:31,363 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,363 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,363 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,364 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,365 DEBUG [sql.AbstractSQLQuery]: select WORKLOG.id, WORKLOG.issueid, WORKLOG.author, WORKLOG.grouplevel, WORKLOG.rolelevel, WORKLOG.worklogbody, WORKLOG.created, WORKLOG.updateauthor, WORKLOG.updated, WORKLOG.startdate, WORKLOG.timeworked, PROJECT_ROLE.id, PROJECT_ROLE.name, PROJECT_ROLE.description from public.worklog WORKLOG left join public.projectrole PROJECT_ROLE on PROJECT_ROLE.id = WORKLOG.rolelevel where WORKLOG.issueid = ? order by WORKLOG.created asc 2019-08-23 01:39:31,365 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,366 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,366 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,366 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,368 DEBUG [sql.AbstractSQLQuery]: select "AO_54307E_ORGANIZATION"."ID", "AO_54307E_ORGANIZATION"."NAME" from "public"."AO_54307E_ORGANIZATION" "AO_54307E_ORGANIZATION" where "AO_54307E_ORGANIZATION"."ID" = ? 2019-08-23 01:39:31,368 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,369 DEBUG [sql.AbstractSQLQuery]: select "AO_56464C_APPROVAL"."ID", "AO_56464C_APPROVAL"."ID", "AO_56464C_APPROVAL"."ISSUE_ID", "AO_56464C_APPROVAL"."NAME", "AO_56464C_APPROVAL"."APPROVE_CONDITION_TYPE", "AO_56464C_APPROVAL"."APPROVE_CONDITION_VALUE", "AO_56464C_APPROVAL"."CREATED_DATE", "AO_56464C_APPROVAL"."COMPLETED_DATE", "AO_56464C_APPROVAL"."DECISION", "AO_56464C_APPROVAL"."STATUS_ID", "AO_56464C_APPROVAL"."SYSTEM_DECIDED", "AO_56464C_APPROVER"."APPROVAL_ID", "AO_56464C_APPROVER"."ID", "AO_56464C_APPROVER"."TYPE", "AO_56464C_APPROVER"."VALUE", "AO_56464C_APPROVERDECISION"."APPROVAL_ID", "AO_56464C_APPROVERDECISION"."DECISION", "AO_56464C_APPROVERDECISION"."ID", "AO_56464C_APPROVERDECISION"."SENT_DATE", "AO_56464C_APPROVERDECISION"."TO_STATUS_ID", "AO_56464C_APPROVERDECISION"."USER_KEY" from "public"."AO_56464C_APPROVAL" "AO_56464C_APPROVAL" left join "public"."AO_56464C_APPROVER" "AO_56464C_APPROVER" on "AO_56464C_APPROVAL"."ID" = "AO_56464C_APPROVER"."APPROVAL_ID" left join "public"."AO_56464C_APPROVERDECISION" "AO_56464C_APPROVERDECISION" on "AO_56464C_APPROVAL"."ID" = "AO_56464C_APPROVERDECISION"."APPROVAL_ID" where "AO_56464C_APPROVAL"."ISSUE_ID" = ? 2019-08-23 01:39:31,371 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,372 DEBUG [sql.AbstractSQLQuery]: select WORKLOG.id, WORKLOG.issueid, WORKLOG.author, WORKLOG.grouplevel, WORKLOG.rolelevel, WORKLOG.worklogbody, WORKLOG.created, WORKLOG.updateauthor, WORKLOG.updated, WORKLOG.startdate, WORKLOG.timeworked, PROJECT_ROLE.id, PROJECT_ROLE.name, PROJECT_ROLE.description from public.worklog WORKLOG left join public.projectrole PROJECT_ROLE on PROJECT_ROLE.id = WORKLOG.rolelevel where WORKLOG.issueid = ? order by WORKLOG.created asc 2019-08-23 01:39:31,373 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,373 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,373 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,374 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,374 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,374 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,375 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,378 DEBUG [sql.AbstractSQLQuery]: select "AO_575BF5_DEV_SUMMARY"."ID", "AO_575BF5_DEV_SUMMARY"."ISSUE_ID", "AO_575BF5_DEV_SUMMARY"."CREATED", "AO_575BF5_DEV_SUMMARY"."UPDATED", "AO_575BF5_DEV_SUMMARY"."PROVIDER_SOURCE_ID", "AO_575BF5_DEV_SUMMARY"."JSON" from "AO_575BF5_DEV_SUMMARY" "AO_575BF5_DEV_SUMMARY" where "AO_575BF5_DEV_SUMMARY"."ISSUE_ID" = ? 2019-08-23 01:39:31,379 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,379 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,379 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,380 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,380 DEBUG [jotm.jta]: Current.getStatus() 2019-08-23 01:39:31,387 DEBUG [support.DefaultListableBeanFactory]: Creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener' 2019-08-23 01:39:31,387 DEBUG [groovyrunner.spring]: BeforeInstantiation [bean=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener, type=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener] 2019-08-23 01:39:31,387 DEBUG [groovyrunner.spring]: AfterInitialisation [bean=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener, type=com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener] 2019-08-23 01:39:31,387 DEBUG [support.DefaultListableBeanFactory]: Finished creating instance of bean 'com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener'
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It's hard to know what's going on when the intentional log outputs are not visible.
Did you copy those log lines from the listener history? Or did you get them directly from the atlassian-jira.log file?
If the former, I would check the file for the "Debug message".
Then, if still not there, remove all the code except the log.error "debug message" and try to again to confirm the event fires and the listener can at least output some logs. Then start to build the logic back up until you find something that stops the outputting of the logs.
Other than that, I noticed something completely unrelated... in the groupOptionMap ... you basically end-up having the same value on the left and right side of the colon.
The intent with that was that on the left, you would have the group name, and on the right, the Team Region field option value. If these are identical, then you don't need a map ... a simple array would suffice:
def regionalGroups = [CSAPA, CSNA, CSEMEA]
def valueToSet = groupManager.getGroupdsForUser(assignee).find{it.value in regionalGroups}?.name ?: NONCS
def optionToSet = availableOptions.find{it.value == valueToSet}
While updating this, I found an error in my original suggestion that would fail to set the default to NONCS when the user is not in a group. This would be better:
def group = groupManager.getGroupsForUser(assignee).find{ it.name in groupOptionMap.keySet()}
def optionToSet = availableOptions.find{ it.value == (group) ? groupOptionMap[group.name] : NONCS }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.