I want to clear the field value during the clone of the issue

kedar December 5, 2020

Hi All,

Any one can help me with code to clear the field value during the clone of the issue in jira.we are using script runner add-on help me with solution.

 

Example : project = xyz and issue type = story and field = Testing .

 

During clone I want to clear the testing custom field value.how it can be done using script runner add-on.

 

Thanks,

Kedar P

 

 

 

4 answers

1 vote
kedar December 6, 2020

Any help on this request 

PW
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 22, 2021

##We tweaked the script to add more variables - works great

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 = 10001
Long TEXT_CUSTOM_FIELD_ID = 12605
Long TEXT_CUSTOM_FIELD_ID2 = 14003
Long TEXT_CUSTOM_FIELD_ID3 = 14004
String ISSUE_TYPE_ID = 10210
String ISSUE_TYPE_ID2 = 10209

IssueLink issueLink = event.getIssueLink()
if (issueLink.getIssueLinkType().getId() != ISSUE_LINK_TYPE_ID) {
return
}
MutableIssue issue = issueLink.getSourceObject()
if (issue.getIssueType().getId() != ISSUE_TYPE_ID && issue.getIssueType().getId() != ISSUE_TYPE_ID2) {
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)
CustomField textCustomField2 = customFieldManager.getCustomFieldObject(TEXT_CUSTOM_FIELD_ID2)
issue.setCustomFieldValue(textCustomField2, null)
CustomField textCustomField3 = customFieldManager.getCustomFieldObject(TEXT_CUSTOM_FIELD_ID3)
issue.setCustomFieldValue(textCustomField3, null)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Like Manuela Davila likes this
1 vote
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.
December 5, 2020

Hi @kedar ,

just to be clear - you are using "Script Post-Function [ScriptRunner]: Clones an issue, and links" post function to clone the issue? If so, how exactly does your configuration of this post function look like? There's "Fields to copy" configuration, where you can select, which fields should be copied to the cloned issue - so maybe you can just skip this field? Which type has your custom field? Thank you for the clarification.

kedar December 5, 2020

Hi Hana,

Thanks for your help.

 

Field type was free text field and during the clone of issue that text field value should cleared.how I can do this

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.
December 6, 2020

Hi @kedar ,

would you please provide me information, how exactly do you perform the clone operation?

kedar December 6, 2020

Hana,

 

We have a project called accounting and in that project we have custom field called accounting which was text field type custom field.so during the clone of issue in that project with bug issue type we need to clear that field value during the clone operation so after clone that cloned issue  should not show any value in that accounting field . 

kedar December 6, 2020

Hi Hana,

We have project called accounting and field called accounting text type field.what we doing was when I perform the clone operation in the accounting project with bug issue type card.accounting field should not have any value it should show as null in cloned issue.

 

Can you help with this.

 

Thansk,

Kedar P

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.
December 7, 2020

Hi @kedar ,

thank you.

Probably you will need to create Custom listener for the event Issue Created and your accounting project. In the script you will have to check, whether the issue was created as a clone (issue is linked with it's source using specific link and has specific summary)

If so, set the custom field to empty string and store the issue.

kedar December 7, 2020

Can you help me with the code how to write using project = accounting and issue type = bug and custom field = accounting (text type)

During clone operation have to clear the field value of accounting.

 

Thanks,

Kedar p 

kedar December 11, 2020

Hana any help on this.

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.
December 11, 2020

Hi @kedar ,

please try to add this as a script listener for your project and IssueLinkCreatedEvent event.

xxxxx needs to by replaced by your Cloners link type id, yyyyy by your text custom field id and zzzzz by your story issue type id

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)

 

Like # people like this
kedar December 12, 2020

Hi Hana,

Thanks for the code.

I have a question regarding this 

 

1. Please let me know that which script listener functionality I should select from the snapshot.

2.After creating the script listener which post function we need to apply on the create transition.

3.what was the issue link type ID.

Please review the snaps.

 

Thanks,

Kedar IMG_20201212_013210.jpgIMG_20201212_010107.jpg

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.
December 12, 2020

Hi @kedar ,

it is Custom listener (first item in the second screenshot).

In the listener's configuration you will need to select your project and also IssueLinkCreatedEvent event.

It should work like this - if issue link is created and it is Cloners link, get the cloned issue, check, if it is a story and if so, clear the text custom field value.

It is little bit tricky, because the only possible way I was able to find how to determine, if the issue was cloned, is the two issues are linked by Cloners link. The clone functionality has two steps - first of all the cloned issue is created and then the issues are linked. That's why IssueLinkCreatedEvent is used instead of IssueCreated.

There is no need to use any additional post function etc.

Sahish March 2, 2022

Hello @Hana Kučerová ,

Hope you have a great day.

I am also having the similar type of requirement.

Thanks for your post, let me try and check.

 

Thanks in advance,

sahish

Sahish March 2, 2022

@Hana Kučerová 

Hi, 

I am also having the similar type of requirement. Could you please help me on this.

I would like to be able to read the System field, especially the field called "Component/s:"

I am providing my code below,

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 = 10005 //Cloning ID//
Long TEXT_CUSTOM_FIELD_ID = underlyingIssue.getComponents() //Component Field ID//
String ISSUE_TYPE_ID = 10601 //Problem Report ID//

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)

Sahish March 6, 2022

Hi @Hana Kučerová ,

 

Anything which you can help me on the above request to clear the system field value during the clone of the issue in jira. we are using script runner add-on.

Example : project = ABC and issue type = Report and field = Component/s

During clone I want to clear the System field Component/s value.

Here I am sharing my code. I used listeners for this.

 

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 = 10005 //Cloning ID//

String ISSUE_TYPE_ID = 10601 //Report ID//

Set issueComponent = issue.getComponent()

 

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()

 

Long textSystemField = issue.getComponent()

issue.setissueComponent(textSystemField, null)

 

issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

 

Thanks,

Sahish

0 votes
Daniel G_ Sinclair February 6, 2023

This is horribly complicated. A simple automation should be able to clear a field on clone...

mummareddy supriya May 14, 2024

can you please guide me ,As how we can clear the fields while cloning through automation?

Thank you in Advance

0 votes
Mayank Kamra October 3, 2021

Hi @Hana Kučerová can you please help me, I have copied your code and replaced the variable id with my instance IDs but getting these below errors:

errors.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.
October 9, 2021

Hi @Mayank Kamra ,

it should work, these are just static type checking.

Have you tried the code?

Mayank Kamra October 10, 2021

Hi @Hana Kučerová 

Thank you so much for replying, sorry my bad I had not tried to run the code but also I didn't know that the code shall run with 'static errors'

I can confirm it worked smoothly as expected, much appreciated :)

Like Hana Kučerová likes this

Suggest an answer

Log in or Sign up to answer