How to write postfunction to add values in a custom field based on values selected in another field

Tennille Noach March 12, 2018

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

1 answer

1 accepted

1 vote
Answer accepted
Kyle Moseley
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 12, 2018

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.

Tennille Noach March 14, 2018

@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?  

Kyle Moseley
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 15, 2018

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

Tennille Noach March 15, 2018

Thanks @Kyle Moseley, I'm hoping for a chunk of undisturbed time this morning to give it a go. Stay tuned!

Tennille Noach March 15, 2018

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?

Kyle Moseley
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 16, 2018

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

Kyle Moseley
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 20, 2018

Hi. If this answer helped, please mark it as an accepted solution using the checkmark next to the answer. Thanks!

Tennille Noach April 4, 2018

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

Tennille Noach April 4, 2018

Does it matter that field I want the manager names added into is a custom field of the type 'User Picker (Multiple Users)'?

Kyle Moseley
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 6, 2018

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.

Like Anshuman Bakshi likes this
Tennille Noach April 9, 2018

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!

Kyle Moseley
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 9, 2018

Glad to hear it. You're welcome.

Suggest an answer

Log in or Sign up to answer