You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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!
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.
Then simply set the fields you wish to clear!
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:-
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:-
As shown below, the Sample Radio and Sample DB field values are visible in the original issue.
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:-
The same test was repeated on the Story Issue type and also works as expected, as shown below:-
I hope this helps to answer your question. :)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you, Ram! This worked for us! My coworker was able to tweak your script and it worked like a charm!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Katie,
I have tried the above script but still it isn't working for us
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
We were able to use a script that he provided above and then edited it a bit and that fixed it. Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.