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

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

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

 

Thanks Katya, this works perfect. 

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

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

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 Sunday in Agility

You asked for it, so we delivered: images on issues have arrived

A picture tells a thousand words. And agility boards have just released their latest feature: cover images on issues – so now your board can tell a story at first glance. Upload attachmen...

133 views 1 10
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