Hi all,
I would really love some help on this, and hopefully not too technical so that I can implement on my own. :) Please find details below which hopefully give enough information for one of you talented people to help me!
I have a custom field called 'Location Managers' which is a multi User Picker field.
I have another custom field called 'Locations to be Accessed' which is a multi Select list. The values in this list appear in the format:
- 'Main building - Basement'
- 'Main building - Vault'
- 'Annexe building - Basement'
- 'Annexe building - Store' etc etc
(There are two grouping of locations in the list, those relating to the Main Building and those relating to the Annexe)
I want to write a post-function(s) on the Create transition to achieve the following:
IF the 'Locations to be Accessed' field contains a one of the 'Main Building' locations then add 'Manager 1' and 'Manager 2' names to the 'Location Managers' field.
IF the 'Locations to be Accessed' field contains a one of the 'Annexe building' locations then add 'Manager 3' and 'Manager 4' names to the 'Location Managers' field.
Thanks in advance,
Tennille
Try something like this after the initial create issue post-function:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def locationsField = customFieldManager.getCustomFieldObjectByName("Locations")
def managersField = customFieldManager.getCustomFieldObjectByName("Managers")
def locations = issue.getCustomFieldValue(locationsField) as List
locations = locations*.toString()
def managers = [] as List
locations.findAll{ it ->
if (it.contains("Site A")) {
managers << ComponentAccessor.getUserManager().getUserByKey("jsmith")
log.debug "site a"
}
if (it.contains("Site B")) {
managers << ComponentAccessor.getUserManager().getUserByKey("fphillips")
log.debug "site b"
}
}
managers = managers.unique()
managersField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(managersField), managers), new DefaultIssueChangeHolder())
This does not account for errors, null values and things like that. So this would just get you started but you will definitely want to refine it.
@Kyle Moseley thanks so much for your reply!
Forgive me but you will have to treat me like the somewhat non-technical admin that I am, and please tell me where exactly do I need to insert this string of code?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Go to the workflow for this project(s), click create's transition (the line) into the first status and add a Script Post-Function post-function. Then you will have a choice of what kind of script. Use Custom script post function. Then enter the script in the inline code section and update/save.
All of this assumes you have ScriptRunner for JIRA.
https://confluence.atlassian.com/adminjiraserver/advanced-workflow-configuration-938847443.html
https://scriptrunner.adaptavist.com/latest/jira/custom-workflow-functions.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks @Kyle Moseley, I'm hoping for a chunk of undisturbed time this morning to give it a go. Stay tuned!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I added in the custom script post function and entered my custom fields and values as such below (field names have changed slightly since my initial enquiry due some internal requests):
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def locationsField = customFieldManager.getCustomFieldObjectByName("Locations to be accessed")
def managersField = customFieldManager.getCustomFieldObjectByName("Managing Collection Stores")
def locations = issue.getCustomFieldValue(locationsField) as List
locations = locations*.toString()
def managers = [] as List
locations.findAll{ it ->
if (it.contains("Harwood Building")) {
managers << ComponentAccessor.getUserManager().getUserByKey("terrym, judithc")
log.debug "Harwood Building"
}
if (it.contains("Castle Hill")) {
managers << ComponentAccessor.getUserManager().getUserByKey("careyw, scottw, jonathanl, judithc")
log.debug "Castle Hill"
}
}
managers = managers.unique()
managersField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(managersField), managers), new DefaultIssueChangeHolder())
I saved the workflow and then created a test issue to try and see if it worked.
Unfortunately it failed, and I got the following error information:
Time (on server): Fri Mar 16 2018 10:13:40 GMT+1100 (AUS Eastern Daylight Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2018-03-16 10:13:39,959 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-03-16 10:13:40,007 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:898) at com.atlassian.jira.issue.Issue$getCustomFieldValue$2.call(Unknown Source) at Script41.run(Script41.groovy:9)
I am unsure which part I need to edit in order to try again?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It cannot find a value for the issue. Make sure the Script occurs after the creation of the issue in the post-function order. At least position #2 in the post function order, at least after "Creates the issue originally."
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi. If this answer helped, please mark it as an accepted solution using the checkmark next to the answer. Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi again, sorry for the delay, Unfortunately even after I moved the script into a lower position in the post function order, i'm still getting an error.
I'm using this script:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def locationsField = customFieldManager.getCustomFieldObjectByName("Locations to be Accessed")
def managersField = customFieldManager.getCustomFieldObjectByName("Managing Collection Stores")
def locations = issue.getCustomFieldValue(locationsField) as List
locations = locations*.toString()
def managers = [] as List
locations.findAll{ it ->
if (it.contains("Harwood")) {
managers << ComponentAccessor.getUserManager().getUserByKey("terrym, judithc")
log.debug "Harwood"
}
if (it.contains("Castle")) {
managers << ComponentAccessor.getUserManager().getUserByKey("careyw, scottw, jonathanl, judithc")
log.debug "Castle"
}
}
managers = managers.unique()
managersField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(managersField), managers), new DefaultIssueChangeHolder())
And i'm getting this error:
Time (on server): Thu Mar 29 2018 08:29:31 GMT+1100 (AUS Eastern Daylight Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2018-03-29 08:29:31,043 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2018-03-29 08:29:31,073 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: REG-2002, actionId: 1, file: <inline script> java.lang.NullPointerException at com.atlassian.jira.issue.customfields.impl.MultiUserCFType.getChangelogValue(MultiUserCFType.java:123) at com.atlassian.jira.issue.customfields.impl.MultiUserCFType.getChangelogValue(MultiUserCFType.java:80) at com.atlassian.jira.issue.fields.ImmutableCustomField.getChangelogValue(ImmutableCustomField.java:375) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:395) at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source) at Script60.run(Script60.groovy:26)
Any thoughts??
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Does it matter that field I want the manager names added into is a custom field of the type 'User Picker (Multiple Users)'?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No, it was accounting for that before. You have multiple usernames when using getUserByKey, you can't do that. Just split those up like so:
managers << ComponentAccessor.getUserManager().getUserByKey("terrym")
managers << ComponentAccessor.getUserManager().getUserByKey("judithc")
Do it with the other option as well.
It worked for me after testing that fix.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
SUCCESS!!!! Thank you so much @Kyle Moseley that last little fix did the trick. I updated the script and then created a test issue and it worked perfectly. Thank you so much for your time and effort in getting this across the line for me - I really appreciate it!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad to hear it. You're welcome.
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.