Add issue key to Summary and Epic Name in a ScriptRunner post function

Frédérique Cusson August 27, 2020

Hi,

I would like to add the issue key at the beginning of the summary and the epic name when an issue is created in the project called SR. 

 

For example : on the create issue page I enter this info

Epic Name : SR Test Epic Name

Summary : SR Test Summary

Lets say that when i click create, the issue key of my issue is SR-101

The "create" post function would change both fields for

Epic Name : SR-101 Test Epic Name

Summary : SR-101 Test Summary

 

I tried this following script, but the field values doesn't change.

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.issueManager
def srEpicName = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10008")
def srSummary = issue.Summary


def strEpicName = (String) issue.getCustomFieldValue(srEpicName)
def strSummary = (String) srSummary
def strIssueKey = (String) issue.key

String newEpicName = strEpicName.replace("SR",strIssueKey)
String newSummary = strSummary.replace("SR",strIssueKey)

issue.setCustomFieldValue(srEpicName, newEpicName)
issue.setSummary(newSummary)

I don't know what's wrong. Can somebody help me please?

Thank you very much

2 answers

1 accepted

0 votes
Answer accepted
Frédérique Cusson August 28, 2020

I managed to make it work. The problem was that I had to save the changes after setting the new values. For whom might be having the same issue, here's my new script.

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue


def issueManager = ComponentAccessor.getIssueManager()

// Update Epic Name
def epicName = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10008")
def strEpicName = (String) issue.getCustomFieldValue(epicName)
def strIssueKey = (String) issue.key
String newEpicName = strEpicName.replace("SR",strIssueKey)

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def textCf2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Epic Name"}

if (textCf2) {
def changeHolder = new DefaultIssueChangeHolder()
textCf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(textCf2), newEpicName),changeHolder)
}

// Update Summary
def strSummary = (String) issue.getSummary()
String newSummary = strSummary.replace("SR",strIssueKey)


MutableIssue mutableIssue = issue as MutableIssue
mutableIssue.setSummary(newSummary)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
Shubhanshu Trivedi February 7, 2022

Hello @Frédérique Cusson  ,

Similar way we have such script where we are creating a sub-task and updating a summary for the sub-taks issue.

For example- 

if Parent JIRA Summary = Parent-Task Summary The subtask has Variant=XYZ, Type = ABC, Sub-Task Summary field = Testing123

then the Subtask Summary should be : [XYZ][ABC] - Parent-Task Testing123

Code is like--

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger

Issue issue = issue
Logger logger = log

if (issue.isSubTask()) {
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField variantCF = customFieldManager.getCustomFieldObjectsByName("Variant").first()
CustomField typeCF = customFieldManager.getCustomFieldObjectsByName("Type").first()


List<Option> variant = issue.getCustomFieldValue(variantCF)
StringBuilder variantString = new StringBuilder("[")
variant?.each {
variantString.append("${it.getValue()},")
}

if(variantString.toString().trim().endsWith(",")){
String temp = variantString.toString()
variantString.replace(temp.lastIndexOf(","), temp.lastIndexOf(",") + 1, "" );
}
variantString.append("]")

String summary = variantString.toString()

Option type = issue.getCustomFieldValue(typeCF)
if(type){
summary += "[${type.getValue()}]"
}

logger.info(issue.getParentObject().getKey())
Issue parentIssue = issue.getParentObject()
summary += " - ${parentIssue.getSummary()} "

logger.info(summary)
issue.summary = summary
updateIssue(issue)
}

void updateIssue(Issue issue) {
MutableIssue issueToUpdate = (MutableIssue) issue
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.DO_NOT_DISPATCH, false)
}

 

 

 

Above code is giving me output - [XYZ][ABC] - Parent-Task

not this 

[XYZ][ABC] - Parent-Task Testing123

can you please help me to find the issue in the above code?

Regards,

Shubhanshu

0 votes
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 27, 2020

Hi @Frédérique Cusson ,

I believe the problem is you just set the new values, but you don't save the changes. I did not test the code below, but it should look something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption

MutableIssue issue = issue
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

CustomField srEpicName = customFieldManager.getCustomFieldObject("customfield_10008")

String strEpicName = issue.getCustomFieldValue(srEpicName)
String strSummary = issue.getSummary()
String strIssueKey = issue.getKey()

String newEpicName = strEpicName.replace("SR", strIssueKey)
String newSummary = strSummary.replace("SR", strIssueKey)

issue.setCustomFieldValue(srEpicName, newEpicName)
issue.setSummary(newSummary)

issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Frédérique Cusson August 28, 2020

Thank you for your answer! However, I get a static warning on that line

MutableIssue issue = issue

Because the variable issue already exist. I tried the code anyway and it doesn't work. I also tried this instead

MutableIssue mutableIssue = issue 

And replaced every "issue" by "mutableIssue, but it doesn't work either. But thanks for the saving part, I'll look into it.

However, it says that the script doesn't even run. Maybe I'm wrong, but even if the script doesn't change anything should it be still running?

postfunction-script-problem.PNG

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 28, 2020

Hi @Frédérique Cusson ,

it is not neccessary to have this row 

MutableIssue issue = issue

in the script, you can remove it. I always add it, because it helps me to check the code in my IDE :-).

In your case, I think you need to change the order of the post functions. The script should be executed after "Creates the issue originally." step (I can see in the picture that it is the first executed post function).

Frédérique Cusson August 28, 2020

@Hana Kučerová Yes I realised that, so I put it in a second transition after that the issue was created, but it still doesn't work:(

Suggest an answer

Log in or Sign up to answer