Adjust Summary on Create Issue with "Issue Type" + "Entered Summary" + "Custom Field 1"

Niklas Gotting October 13, 2016

At Create Issue I want the Summary to be a combination of a few other fields entered in the Create form. Basically I want "Issue Type" + "Entered Summary" +  "Custom Field 1" etc

I have Script Runner installed and I have managed to get this script to work:

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
issue.summary = "HR Request Test - " + issue.summary;

And this script is placed in the Create function under Post Functions like this:

Creates the issue originally.
Script workflow function : A custom script will be run:
Adjust name to hardcoded "HR Request Test - " + Summary
from inline script.
Re-index an issue to keep indexes in sync with the database.
Fire a Issue Created event that can be processed by the listeners.

In my project I have 4 Issue Types and some custom fields apart from ordinary JIRA native fields

This is a HR Request scenario, so it's like this in pseudo

"Issue Type" + "CF1" + CF2 + "Entered Summary"

Examples:
"New Headcount;" "Title" + "org-unit" + "Entered Summary"
"Replacement current HC New role;" + "Title New" + "org-unit" + "Entered Summary"
"Replacement current HC Same role;" + "Title" + org-unit" + "Entered Summary"
"Change compensation;" "First name" + "Last name" + "Title" + "Entered Summary"

So the question is how to get Issue Type-value and CF-values into the script

 

3 answers

1 accepted

0 votes
Answer accepted
Katya Wegner October 17, 2016

Hi, you can read out the value of a customField for example like this:

def customField1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'customField1'}

and then use the variable "customField1" in 

issue.summary = "HR Request Test - " + issue.summary + ": " + customField1;

And if you are on the page where you put in your code, you can get some script examples, too.

image2016-10-17 14:29:24.png

 

Niklas Gotting October 17, 2016

Thanks Katya, this works perfect. 

0 votes
Niklas Gotting October 17, 2016

I ended up making the the script a bit more complex, since I needed to check and fix summary differently depending on Issue Type. Also since Summary is ALWAYS mandatory and it must be on the Create screen, I added a default according to this KB-article:

https://confluence.atlassian.com/jirakb/how-to-set-default-value-for-summary-field-in-jira-800692650.html

This is what my script looks like:

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.component.ComponentAccessor;
// Define Custom fields to use
String FIRSTNAME = "First Name";
String LASTNAME = "Last Name";
String ORGUNIT = "Org-unit"
String TITLE = "Title"
String TITLENEW = "Title New"
//Issue Types
final String CHGCOMP = "Change Compensation";
final String NEWHEAD = "New Headcount";
final String HCNEWROLE = "Replace Current HC New Role";
final String HCSAMEROLE = "Replace Current HC Same Role";
// Get values from the create screen to use
String ISSUETYPEUSED = issue.issueType.name;
// Get values from First Name, Last Name, Org-unit, Title, Title New entered in the create screen
CustomFieldManager customFieldManager1 = ComponentAccessor.getCustomFieldManager();
CustomField CF1 = customFieldManager1.getCustomFieldObjectByName(FIRSTNAME);
def FIRSTNAMEValue = issue.getCustomFieldValue(CF1);
CustomFieldManager customFieldManager2 = ComponentAccessor.getCustomFieldManager();
CustomField CF2 = customFieldManager2.getCustomFieldObjectByName(LASTNAME);
def LASTNAMEValue = issue.getCustomFieldValue(CF2);
CustomFieldManager customFieldManager3 = ComponentAccessor.getCustomFieldManager();
CustomField CF3 = customFieldManager3.getCustomFieldObjectByName(ORGUNIT);
def ORGUNITValue = issue.getCustomFieldValue(CF3);
CustomFieldManager customFieldManager4 = ComponentAccessor.getCustomFieldManager();
CustomField CF4 = customFieldManager4.getCustomFieldObjectByName(TITLE);
def TITLEValue = issue.getCustomFieldValue(CF4);
CustomFieldManager customFieldManager5 = ComponentAccessor.getCustomFieldManager();
CustomField CF5 = customFieldManager5.getCustomFieldObjectByName(TITLENEW);
def TITLENEWValue = issue.getCustomFieldValue(CF5);
//Default Corrupt Summary if missing Issue Type
issue.summary = ISSUETYPEUSED + "- Missing in WF-" + issue.summary;
//CHECK REQUEST TYPE AND BUILD SUMMARY STRING
if(ISSUETYPEUSED == CHGCOMP){
issue.summary = ISSUETYPEUSED + " - " + FIRSTNAMEValue + " " + LASTNAMEValue + " - "+ TITLEValue;
}
if(ISSUETYPEUSED == NEWHEAD){
issue.summary = ISSUETYPEUSED + " - " + TITLEValue + " - " + ORGUNITValue;
}
if(ISSUETYPEUSED == HCNEWROLE){
issue.summary = ISSUETYPEUSED + " - " + TITLENEWValue + " - " + ORGUNITValue;
}
if(ISSUETYPEUSED == HCSAMEROLE){
issue.summary = ISSUETYPEUSED + " - " + TITLEValue + " - " + ORGUNITValue;
}
0 votes
Niklas Gotting October 13, 2016

Forgot to mention that we are using JIRA 7.2.1 and Script Runner 4.3.9

Suggest an answer

Log in or Sign up to answer