script field update when issue is created based on custom field

I have a custom field called "location" that is a dropdown with the value options being Building A, Building B, and Building C. When a customer is creating an issue and they choose their location, I would like it to automatically update another custom field called "Team" with the corresponding team assigned to that location.

For example, when a customer chooses "Building A" when they create their issue, I would like it to automatically populate the team field with "Team A." Same for Building B-Team B and Building C-Team C.

Is this achievable with ScriptRunner?

2 answers

1 accepted

Thank you for the help. I have actually solved the issue with the Workflow plugin with the following rules (below) for 'Set a field as a function of other fields Function.' and setting the Location field as the Field to be Checked and the Team field as the Target Field. Seemed to be the simplest and quickest way to accomplish. 

(Building A)Team A

(Building B)Team B

(Building C)Team C

You can do this with Custom Listener that triggers on Issue Created event. This requires a script in Groovy though.

If you want to take a stab at it and already have ScriptRunner installed go to: https://yourjirainstance.com/plugins/servlet/scriptrunner/builtin?section=script_listeners (of course change "yourjirainstance.com" with address to your Jira) and create Custom Listener with following Inline Script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def currentIssue = event.issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def locationField = customFieldManager.getCustomFieldObjectByName("Location");
def teamField = customFieldManager.getCustomFieldObjectByName("Team")
def locationFieldValue = currentIssue.getCustomFieldValue(locationField);
def locationFieldValueString = locationFieldValue.toString()
def optionsManager = ComponentAccessor.getOptionsManager();
def changeHolder = new DefaultIssueChangeHolder();

if (locationFieldValueString == "Building A") {
    def teamFieldOption = optionsManager.getOptions(teamField.getRelevantConfig(currentIssue)).find {it.value == "Team A"};
 teamField.updateValue(null, currentIssue, new ModifiedValue(currentIssue.getCustomFieldValue(teamField), teamFieldOption), changeHolder);
} else if (locationFieldValueString == "Building B") {
    def teamFieldOption = optionsManager.getOptions(teamField.getRelevantConfig(currentIssue)).find {it.value == "Team B"};
 teamField.updateValue(null, currentIssue, new ModifiedValue(currentIssue.getCustomFieldValue(teamField), teamFieldOption), changeHolder);
} else if (locationFieldValueString == "Building C") {
    def teamFieldOption = optionsManager.getOptions(teamField.getRelevantConfig(currentIssue)).find {it.value == "Team C"};
 teamField.updateValue(null, currentIssue, new ModifiedValue(currentIssue.getCustomFieldValue(teamField), teamFieldOption), changeHolder);    
}

 

Note that only recent versions of ScriptRunner have inline scripting functionality (otherwise you have to save script to file and provide path to the script accessible on the server).

I didn't test the code but this is the general gist.

Best of luck and let us know if you have solved your problem (and how)!

Does it make a difference if the Team custom field is a group picker? The code runs with no errors, but it doesn't actually assign the Team value. Is this because it has the field type of "group picker"?

I don't have experience with "group picker" but this works for "user picker" so I assume code will be somewhat similar:

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def currentIssue = event.issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def optionsManager = ComponentAccessor.getOptionsManager();
def changeHolder = new DefaultIssueChangeHolder();
def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName("username");
def userField = customFieldManager.getCustomFieldObjects(currentIssue).find {it.name == 'User Picker'};
userField.updateValue(null, currentIssue, new ModifiedValue(currentIssue.getCustomFieldValue(userField), user), changeHolder);

 

Can anyone provide me a sample syntax for  this logic?

IF dropdown_field = option3 or option4 then Text_field =Required END IF

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

340 views 2 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you