How to clear custom fields when cloning an issue

Katie_R June 8, 2022

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!

3 answers

2 accepted

2 votes
Answer accepted
Haris Vugdalic March 22, 2023

I know this is already solved but another possible, and simpler, solution could be to clear the field(s) in the workflow as a post-function using JMWE or the built-in field clearing mechanism on the on-create transition.

Note: Cloned issues also traverse this transition.

Screenshot 2023-03-22 at 10.38.11 AM.png

Screenshot 2023-03-22 at 10.38.57 AM.png

 

Then simply set the fields you wish to clear!

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 10, 2022

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

Katie_R June 10, 2022

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

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 10, 2022

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

Katie_R June 27, 2022

Thank you, Ram! This worked for us! My coworker was able to tweak your script and it worked like a charm! 

Akshith Kumar August 30, 2022

Hi Katie,

I have tried the above script but still it isn't working for us

Akshith Kumar August 30, 2022

I tried his script but this script is clearing the field values while creating issues as well I want to clear field values only when we clone the issues

Katie_R September 1, 2022

Are the fields being filled in on creation? If not, that should work for you. Otherwise, I unfortunately do not have another solution, as it was my coworker who wrote the script I shared. I do not have the experience with ScriptRunner that he does.

0 votes
Akshith Kumar August 29, 2022

Hi @Ram Kumar Aravindakshan _Adaptavist_ 
I want to clear some custom field values while cloning issues in Jira(Data center). We have Automation for Jira and script runner plugins can you provide any script to achieve this.

Katie_R August 30, 2022

We were able to use a script that he provided above and then edited it a bit and that fixed it. Thank you!

Akshith Kumar August 30, 2022

Hi Katie,

Can you provide your script it may be helpful for us

Katie_R August 30, 2022

Akshith,

This is what we ended up using so that it would clear three fields upon cloning for any of the issue types in Jira. We are using Jira Data Center. 

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

//ISSUE_TYPE_ID1 = Task
//ISSUE_TYPE_ID2 = Improvement
//ISSUE_TYPE_ID3 = Bug
//ISSUE_TYPE_ID4 = Code Task
//ISSUE_TYPE_ID5 = Sub-task
//ISSUE_TYPE_ID6 = Task
//ISSUE_TYPE_ID7 = Story
//ISSUE_TYPE_ID8 = Epic
//ISSUE_TYPE_ID9 = SWAD
//ISSUE_TYPE_ID10 = Technical Debt
//ISSUE_TYPE_ID11 = TOR
//ISSUE_TYPE_ID12 = New Feature
//ISSUE_TYPE_ID13 = SPIKE
//ISSUE_TYPE_ID14 = Technical Task
//ISSUE_TYPE_ID15 = Software
//ISSUE_TYPE_ID16 = Non-Software
//ISSUE_TYPE_ID17 = Code Release
//ISSUE_TYPE_ID18 = New Feature

 


Long ISSUE_LINK_TYPE_ID = 10001
Long CUSTOM_FIELD_ID = 12101
Long CUSTOM_FIELD_ID2 = 12005
Long CUSTOM_FIELD_ID3 = 11803
String ISSUE_TYPE_ID1 = "10002"
String ISSUE_TYPE_ID2 = "10101"
String ISSUE_TYPE_ID3 = "10100"
String ISSUE_TYPE_ID4 = "10600"
String ISSUE_TYPE_ID5 = "10003"
String ISSUE_TYPE_ID6 = "10002"
String ISSUE_TYPE_ID7 = "10001"
String ISSUE_TYPE_ID8 = "10000"
String ISSUE_TYPE_ID9 = "10200"
String ISSUE_TYPE_ID10= "10104"
String ISSUE_TYPE_ID11= "10500"
String ISSUE_TYPE_ID12= "10102"
String ISSUE_TYPE_ID13= "10103"
String ISSUE_TYPE_ID14= "10105"
String ISSUE_TYPE_ID15= "10201"
String ISSUE_TYPE_ID16= "10202"
String ISSUE_TYPE_ID17= "10300"
String ISSUE_TYPE_ID18= "10102"

 

IssueLink issueLink = event.getIssueLink()
if (issueLink.getIssueLinkType().getId() != ISSUE_LINK_TYPE_ID){
return
}
MutableIssue issue = issueLink.getSourceObject()
if (issue.getIssueType().getId() != ISSUE_TYPE_ID1 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID2 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID3 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID4 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID5 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID6 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID7 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID8 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID9 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID10 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID11 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID12 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID13 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID14 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID15 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID16 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID17 &&
issue.getIssueType().getId() != ISSUE_TYPE_ID18) {
return
}
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField CustomField = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID)
issue.setCustomFieldValue(CustomField, null)
CustomField CustomField2 = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID2)
issue.setCustomFieldValue(CustomField2, null)
CustomField CustomField3 = customFieldManager.getCustomFieldObject(CUSTOM_FIELD_ID3)
issue.setCustomFieldValue(CustomField3, null)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Velkuri Balaji July 12, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

 

We would like to implement same that need to clear some fields on cloned issue but we have script runner to implement but could you please provide a script for Jira Cloud so that we can set script under script listeners 

Thanks in advance,

Balaji

Suggest an answer

Log in or Sign up to answer