Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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?  

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

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

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?

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

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

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

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

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

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!

Glad to hear it. You're welcome.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you