Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,293,546
Community Members
 
Community Events
165
Community Groups

How to clear custom fields when cloning an issue

Our customer has asked if there is a way to clear two custom fields when an issue is cloned. We are using Jira Data Center 8.20.5 and have ScriptRunner. We did find a script in the community that works for this, but it uses a specific issue type so it won't work for us. This would be for cloning any issue type. The two fields are a radio button and a database picker that goes into a specific table and then allows you to select one or more sprints from the table. Here is the script we are starting with that we got from another community post but we can't seem to figure out how to correct it to what we want:

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

Long ISSUE_LINK_TYPE_ID = xxxxx
Long TEXT_CUSTOM_FIELD_ID = yyyyy
String ISSUE_TYPE_ID = "zzzzz"

IssueLink issueLink = event.getIssueLink()
if (issueLink.getIssueLinkType().getId() != ISSUE_LINK_TYPE_ID) {
return
}
MutableIssue issue = issueLink.getSourceObject()
if (issue.getIssueType().getId() != ISSUE_TYPE_ID) {
return
}
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField textCustomField = customFieldManager.getCustomFieldObject(TEXT_CUSTOM_FIELD_ID)
issue.setCustomFieldValue(textCustomField, null)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Any assistance is appreciated. Thank you!

1 answer

Hi @Katie_R

Could you provide some clarification, i.e. on which issue do they want the field to be cleared, on the original issue or the cloned issue?

Thank you and Kind regards,

Ram

Hello Ram,

We are trying to clear the fields on the cloned issue so everything is cloned except these two custom fields.

Thank you!

Katie

Hi @Katie_R

For your requirement, you could try something like this:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issue = event.issueLink.sourceObject as MutableIssue

def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def sampleRadio = customFieldManager.getCustomFieldObjectsByName('Sample Radio').first()
def sampleDB = customFieldManager.getCustomFieldObjectsByName('Sample DB').first()

issue.setCustomFieldValue(sampleRadio, null)
issue.setCustomFieldValue(sampleDB, null)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications. 

The approach above is not restrictive to a specific issue type. Hence you will be able to use it for any issue type.

Below is a screenshot of the Listener configuration:-

issue_link_event.png

 

Below are a few test screens:-

1. When the original issue is created, the values for the Radio button (Sample Radio) and DB Picker (Sample DB) are set to Yes and Service Test, respectively, as shown below:-

issue_creation.png

As shown below, the Sample Radio and Sample DB field values are visible in the original issue.

original_issue.png

Once the issue is cloned, the values of the fields are removed and hidden from the view screen of the Cloned issue, as shown below:-

clone_issue.png

The same test was repeated on the Story Issue type and also works as expected, as shown below:-

original_story.pngclone_story.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Jira Software

Upcoming changes to epic fields in company-managed projects

👋 Hi there Jira Community! A few months ago we shared with you plans around renaming epics in your company-managed projects. As part of these changes, we highlighted upcoming changes to epics on...

14,113 views 34 44
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you