script field update when issue is created based on custom field

CMCSS Programmers April 4, 2017

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

0 votes
Answer accepted
CMCSS Programmers April 4, 2017

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

1 vote
Ron Gates April 4, 2017

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)!

CMCSS Programmers April 4, 2017

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

Ron Gates April 4, 2017

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);

 

Ratna Kumar Lekkala June 26, 2017

Can anyone provide me a sample syntax for  this logic?

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

Kumar
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.
January 16, 2019

Hi @Ron Gates  

I have similar issue Can you please help me with the Script my requirement  is

I have Group Field called "Assignment Group"

Group X : A,B,C,D

Group Y : E,F,G,H

Group Z : I,J,K,L

So in a project if any user is Creating Issue or Issue Updating  if that issue is assignee to " A " user the group filed has to automatically update to Group "X".

same like that depends up on assignee user the group filed has to change to user related group

I have modified the above script and I performed on Custom  Listener

 

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 assigneeField = customFieldManager.getCustomFieldObjectByName("assignee");
def assignmentgroupField = customFieldManager.getCustomFieldObjectByName("Assignment Group")
def assigneeFieldValue = currentIssue.getCustomFieldValue(assigneeField);
def assigneeFieldValueString = assigneeFieldValue.toString()
def optionsManager = ComponentAccessor.getOptionsManager();
def changeHolder = new DefaultIssueChangeHolder();

if (assigneeFieldValueString == "kumar") {
def assignmentgroupFieldOption = optionsManager.getOptions(assignmentgroupField.getRelevantConfig(currentIssue)).find {it.value == "Incident-Developer1"};
assignmentgroupField.updateValue(null, currentIssue, new ModifiedValue(currentIssue.getCustomFieldValue(assignmentgroupField), assignmentgroupFieldOption), changeHolder);
}

 here is the Logs :

2019-01-16 16:40:58,911 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2019-01-16 16:40:58,912 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NullPointerException
 at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
 at com.atlassian.jira.issue.Issue$getCustomFieldValue$5.call(Unknown Source)
 at Script930.run(Script930.groovy:9)

 

the script does not have any errors but when i assign the Issue to my id the group field not generating the value

Can you please help me here

 

Thanks,

Kumar

Suji Shyam June 29, 2020
def currentIssue = event.issue;

 

This throws "Static type checking error" for events as undeclared in Scriptrunner console.

Any alternate method there ! to capture the event and update the fields via Scriptrunner Console. 

Suggest an answer

Log in or Sign up to answer