ScriptRunner, automation rules - add a value to a custom field list based on field value

Whitni Smith August 15, 2018

Hi!

I'm very new to using ScriptRunner for custom scripts, we use the built-in scripts frequently.

We have a custom field (drop down list, select one) of vendors and publishers that is constantly needing values added and our users submit tickets for these requests. We currently have in the request form, a field for the vendor/publisher name and a set of checkboxes (Publisher or Vendor) that the user can select which one they want to add.

I have to keep adding this in manually and it's a bit tedious. I was looking at Automation for Jira (we have the plugin) and I noticed the ability to execute a ScriptRunner script based  on a condition.

Custom fields:

  • Vendor/Publisher name; Summary field
  • Publisher/Vendor; Checkboxes
  • Publisher(s); select list (single choice)
  • Vendor(s): select list (single choice)

What I am hoping to achieve:

[Can accomplish this with automation for JIRA] If issue created, "customer request type" = "Add Publisher/Vendor" and assignee is "administrator"

Then

[Hoping to accomplish this with ScriptRunner script] If publisher check box is selected, add the text in the vendor/publisher name field to the publishers custom field (that lists all publishers)

if vendor check box is selected, add the text in the vendor/publisher name field to the vendors custom field (that lists all publishers)

Thank you in advance!

Whitni

1 answer

0 votes
Roland Holban (Adaptavist)
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.
August 15, 2018

Dont use Automation for JIRA here. Instead, create a custom script post function on the Create transition. Here is an example script you can build off of. It showcases every step necessary in achieving your goal.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext

def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def selectList = customFieldManager.getCustomFieldObjectsByName("SELECT_LIST_NAME")[0]
def selectListFieldConfig = selectList.getRelevantConfig(IssueContext.GLOBAL)

def options = optionsManager.getOptions(selectListFieldConfig)

if (!options.any { it.value == cfValues["VENDOR_FIELD_NAME"] }) {
optionsManager.createOption(selectListFieldConfig, null, null, cfValues["VENDOR_FIELD_NAME"])
}

So every time an issue is created in that workflow, this post-function will check if the vendor name is already one of the options available in the select list, and if its not, it will permanently add it.

Whitni Smith August 15, 2018

It's possible I'm not understanding the script correctly but I don't think this will work given how we use the vendor and publisher fields and they aren't filled in at the create transition. They aren't used at the create field, except only with the customer request type "Add Publisher/Vendor", and it's only one text field for the publisher or vendor, so a user would submit the request type, enter in the name of the company and then mark (using the check boxes) whether they are a publisher or vendor or both and that it needs to be added so they can track it on a certain ticket. 

After writing that, let me back up and give context - we are processing book/article purchase requests, for reporting purposes we track the vendor/publisher. These values sit in two separate custom fields (single choice select lists) and currently there's no field to manually enter this information (if it's not available in the drop down list) in the issue types that they would need to track it but can be easily added if needed. I thought a custom script could help with this but TBH I couldn't figure out how to write a script that would look at the manual text field, match it to the select-list and add it if not there and update the select list (real time) so the user can select it from the drop down menu while editing the ticket. If I can eliminate the extra request type (Add Publisher/Vendor) and automate adding new vendor/publisher values to the correct lists then I'm all for that. 

Roland Holban (Adaptavist)
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.
August 15, 2018

Ok, I have an idea how to solve this, but before I do, let me make sure I understand correctly.

  • You have one text field: Vendor/Publisher name
  • You have one checkbox: Are you vendor, publisher, or both?
  • You have one select list: List of vendors
  • You have another select list: List of publishers

If the above is correct, we can use a Behaviour script on the checkbox field that takes the value of the name text field, and depending on the checkbox selection, adds a new option (in real time) to vendors, publishers, or both if that is not already an exisiting option.

Sound good?

Whitni Smith August 15, 2018

Sort of correct, but listing the custom fields from two request types here the behaviour I listed in the second paragraph is where the vendor and publisher select lists sit.

The work around, which is individually requesting vendor and/or publisher to be added to the necessary select lists, I then have to manually add them and once added, requester is alerted (resolved ticket) and then goes back and updates the ticket that caused the request to add.

To achieve the behaviour, I believe I'd need to add the text field and check boxes to the primary request (the one that causes the work around) or I can add text field for publisher (if not available in the list) and a text field for vendor (if not available in the list) -- not sure which would be easier to script.

Roland Holban (Adaptavist)
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.
August 15, 2018

Im sorry but I cannot wrap my head around what youre saying. Sending some screenshots of both the request types forms might help. 

My solution above assumed all the fields were on the same request type, same form.

Whitni Smith August 15, 2018

Definitely, sorry for the confusion, it started out automating a request type that's in place as a work around for adding values to a drop down list and then I asked that if we could create a solution that would allow me to remove that all together and just make the changes in real time on the primary form we need it for.

Let's do this, let's assume one form will have the following custom fields:

 

  • one select list: List of vendors
  • another select list: List of publishers
  • Text1 field that shows up when "Other" is the selected option in the list of vendors; hidden otherwise
  • Text2 field that shows up when "Other" is the selected option in the list of publishers; hidden otherwise

Behavior I want to automate, if other is selected, user types value into the now visible field which then adds the value to the respected list in real time so they can then select the value from the list.

Roland Holban (Adaptavist)
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.
August 15, 2018

I understand now :)

You can accomplish your goal with ScriptRunner alone, but there are some serious drawbacks you need to consider.

For example, lets say a user chooses Other and the text field pops up. The way the script would work is that every time the user clicks outside the text field, whatever text was in the field would automatically get added to JIRA's database as an option for that select list. They could've simply made a typo then go back and add another option, or they could just add a bunch of garbage options deliberately. Either way, your select list is now getting cluttered up with a bunch of meaningless options. Even more than that, its never a good idea to give users write access (whether they know they have it or not) to your JIRA instance as this opens you up to numerous security risks.

I cant think of a way to update the options of your select lists in real time without exposing yourself to what I said above. You might have to come up with a different approach to your use case...

Whitni Smith August 16, 2018

Understood, I'd wondered if that would be the case.

Then looks like we'll keep the work around, would be awesome if I could automate their submissions so I don't have to manually add it every time they requested it.

Roland Holban (Adaptavist)
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.
August 16, 2018

What do you think of a custom post-function that operates immediately after the issue has been created?

It would first check if the Other option is selected in the select lists, then it would check if the text in the text field is already an option in the select list, if its not it would add it as one and select it, then lastly it would hide the text field.

It obviously not be real-time, but it would eliminate some of the drawbacks...

Whitni Smith August 16, 2018

That could definitely work, given that we often don't have the vendor/publisher upon creation (gets added later), I'd just have to put it as a post function in the workflow transition that the information would have been added.

Roland Holban (Adaptavist)
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.
August 16, 2018

If you want, I can give you a script that would accomplish what I said in my previous comment. Of course that will only work directly after issue creation. So if the user edits the issue and adds the vendor name later, well then you would have to add it manually.

Whitni Smith August 16, 2018

Sure, we can try that!

Roland Holban (Adaptavist)
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.
August 17, 2018

This is a bit involved so follow along carefully. Before starting you will need to make sure your vendor select list and your vendor name text field are on the same form.

 

1. Create a behaviour that initially hides the field in the initialiser, then on the select list field add a script that unhides it and makes it required if select list value is Other. 

Screen Shot 2018-08-17 at 8.42.59 AM.png

 

2. Create a custom script post function on the "Create" transition. Use this script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.context.IssueContext
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig

// Global variables
customFieldManager = ComponentAccessor.customFieldManager
optionsManager = ComponentAccessor.optionsManager
issueService = ComponentAccessor.issueService
currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def selectList = customFieldManager.getCustomFieldObjectsByName("Vendor")[0]
def selectListFieldConfig = selectList.getRelevantConfig(IssueContext.GLOBAL)
def selectListValue = issue.getCustomFieldValue(selectList).value

if (selectListValue == "Other") {
def textField = customFieldManager.getCustomFieldObjectsByName("Vendor Name")[0]
def vendor = issue.getCustomFieldValue(textField).value as String

// Create the text fields value as an option in the database
createOption(selectListFieldConfig, vendor)

// Set the select lists value to the new option
updateSelectList(selectList, selectListFieldConfig, vendor)

// Clear the text field so it becomes hidden on the issue view screen
clearTextField(textField)
}


def createOption(FieldConfig fieldConfig, String optionName) {
optionsManager.createOption(fieldConfig, null, null, optionName)
}


def updateSelectList(CustomField selectList, FieldConfig fieldConfig, String optionName) {
def option = optionsManager.getOptions(fieldConfig).find { it.value == optionName }

def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.addCustomFieldValue(selectList.id, option.optionId.toString())

def update = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)

if (update.valid) {
issueService.update(currentUser, update)
}
}


def clearTextField(CustomField field) {
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.addCustomFieldValue(field.id, "")

def update = issueService.validateUpdate(currentUser, issue.id, issueInputParameters)

if (update.valid) {
issueService.update(currentUser, update)
}
}

 

3. Make sure the post-function is right after re-indexing in the execution order:

Screen Shot 2018-08-17 at 8.48.49 AM.png

 

NOTE: This script is only for one select list (named "Vendor") and one text field (named "Vendor Name"). I know in your use case you have two of each so you will have to slightly tweak the script to work with both.

Like Tural Aliyev likes this
Tural Aliyev April 8, 2020

Dear @Roland Holban (Adaptavist) ,

Could you please take a look to this?

https://community.atlassian.com/t5/Jira-questions/Automation-of-Definition-of-Done-custom-field/qaq-p/1344685

I would really appreciate it! Thank you in advance.

Best Regards,

Tural

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events