Exclude or clear custom field for a cloned issue

Suresh Bhandiwad September 16, 2019

Team,

We are using the Jira cloud environment, we have couple of custom field which we don't want to be copied when cloned using Jira in-built functionality. The only possibility I see here is using Jira script runner plugin by configuring the listner. Could anyone help me with the script? 

Environment: Jira cloud
Custom field, Single line text field

Seems cloud script runner works in a different way than the Jira server. 

Thanks,
Suresh

5 answers

1 accepted

3 votes
Answer accepted
Kristian Walker _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.
September 16, 2019

Hi Suresh,

Thank you for your question.

I can confirm that to have a field value removed when an issue is cloned using the built-in Jira clone issue functionality with ScriptRunner for Jira Cloud that you will need to create a Script Listener and configured to fire on the Issue Link Created event. 

The script for the listener will need to check the type of the issue link that is created and if it has the link type of cloners then will need to update the cloned issue to make a put request to the Issue  rest API in order to update the fields that you want to set to have a null value. 

I have created an example script located here which you can configure as a script listener on the  Issue Link Created event in order to clear the value of the Description field and a specified Single line text field on the cloned issue.

You will then be able to use this script as a reference guide and to modify this example in order to set other fields that you want to remove the values from when the issue is cloned.

If this response has answered your question can you please mark it as accepted so that other users can see it is correct when searching for similar answers.

Regards,

Kristian

Kristian Walker _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.
September 16, 2019

Hi Suresh,

I can confirm that I have updated my example above to show how to remove the value from a single line select list type custom field as well. 

Regards,

Kristian

Like # people like this
Suresh Bhandiwad September 16, 2019

Thank you for your response Kristian!

I see below static error when configured and ended in failure. IssueLink not declared.  Is there something I need to add further?

2019-09-16 23_48_18-Script Listeners - JIRA.png

Attached screenshot.

Kristian Walker _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.
September 17, 2019

Hi Suresh,

Can I please ask you to confirm if you set your Script Listener to be configured on the IssueLink Created event as the listener will need to be configured on this event in order for the issueLink variable to be available to the script.

I can confirm that if you use the Issue Created event which is similar that you will get this static type checking warning and that you should change the event to be the  IssueLink Created event to resolve this warning.

Regards,

Kristian

Like Darren Xie likes this
Suresh Bhandiwad September 17, 2019

Hi Kristian,

It worked like a charm!

Yes, you're correct. I configured for Issue Created which caused static type checking warning ending up in failure. Changing the Event to IssueLink Created resolved issue.

Thank you so much!

Thanks,
Suresh

Suresh Bhandiwad September 19, 2019

Hi Kristian,

Listener works fine as intended. However, we observer that page loads earlier than the script executes. Then refresh of the page would clear these fields. Is there way to avoid loading of the page before script executes or alternate way to implement this using issuecreated event to avoid this lag.

Please advise.

Thanks,
Suresh

Kristian Walker _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.
September 19, 2019

Hi Suresh,

Thank you for your response.

I can confirm that unfortunately, the reason the issue loads before the listener is run is due to the fact that Atlassian has designed Jira Cloud to execute in a completely asynchronous manner as described inside of our documentation pages located here and here

This, unfortunately, means that there is no way that we can avoid the issue screen loading before the script listener has run to avoid users having to refresh the screen to see the values cleared.

As explained this caused by the way that Jira Cloud is architected and as this how Atlassian make it run then there is no way to work around this limitation.

Regards,

Kristian

Like Darren Xie likes this
Sayan Kaiser March 24, 2020

Hi Kristian,

I am trying to use your script but I am having the static error issue, please be advised I am using Jira on Prem therefore will the script needs to be modified ?

Suresh Bhandiwad March 25, 2020

Hi Sayan,

Same script doesn't work with on-prem. You need to modify the script. Try below with few modifcations.

https://community.atlassian.com/t5/Jira-questions/Clearing-fields-on-cloned-issues-during-cloning-action/qaq-p/93315

Kristian Walker _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.
March 25, 2020

Hi Sayan,

My example on this post is only for Jira Cloud and so will not work with Jira Server.

Regards,

Kristian

Hristina Valcheva November 11, 2020

@Kristian Walker _Adaptavist_ 

Can you please give an example code with cleaning a number and select list single choice field types?
Thank you!

Regards

Hristina

Kristian Walker _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.
November 11, 2020

Hi Hrinsta,

The example above should work and you should be able to just change the field type and set null for both field types to clear a number field and a single select list field.

Regards,

Kristian

Hristina Valcheva November 11, 2020

Great, thanks!

Kristian Walker _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.
November 12, 2020

Hi Hrinsta,

I can confirm I have updated the example linked above to also show how to clear number fields and single select list fields.

Regards,

Kristian

Hristina Valcheva November 12, 2020

Hi,

 

Yes, noted.

Thanks!

Suresh Bhandiwad January 18, 2021

Hi @Kristian Walker _Adaptavist_ ,

Above script does not work for the servicedesk projects in jira cloud. Can yiu please help how we can clear the value of text field for the field in ServiceDesk project?

Thanks,
Suresh

Kristian Walker _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.
January 18, 2021

Hi Suresh,

Can I please ask when you run the script on a Service Desk Project are you updating the issue inside of Jira or via the service desk customer portal?

If it is via the customer portal, then I can confirm that it is not possible to run a listener to clear values based on actions in the customer portal.

If you are updating the project from inside of Jira and getting errors then can you please attach a screenshot showing how the listener is configured and what error you see when you run it?

I can confirm I have tested the script on a service desk project and can confirm it will work as expected if you configure it on the *issuelink Created* event and trigger it using the built-in clone issue function inside of Jira as shown below.

Screenshot 2021-01-18 at 15.55.07.png

This means that the script is correct and the reason it is not working in your instance is due to the fact it has not been configured correctly or triggered correctly.

Regards,
Kristian

Suresh Bhandiwad January 18, 2021

Hi Kristian,

Yes, you're correct. It is configured correctly however we are passing two fields to clear the values simultaneously. Since, the other field failes whole script terminates with failure. Thank you for the quick response.

Thanks,
Suresh

Kristian Walker _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.
January 18, 2021

Hi Suresh,

This indicates you have not configured the script correctly on your instance to handle the fields which you are clearing as it either is not finding the field or you have not specified the correct syntax to clear that type of field.

Can you please attach a screenshot showing how you have configured the script as well as what the error you get when you run the script is so that I can see how the script is configured and what error you are receiving as without this then I cannot advise on the error you are getting?

Regards,

Kristian

Suresh Bhandiwad January 18, 2021

Hi Kristian,

Please find the attached screenshots & they as below:

Config - 01: Shows the projects and events its configured
Script - Its the same script you had shared earlier
History - Shows all were successful
Execution History shows some logs

Salesforce CR ID: customfield_11950

Code Drop ID - Fixed: customfield_11911

 

2021-01-18 15:24:29.016 INFO - Serializing object into 'interface java.util.List'
2021-01-18 15:24:29.071 INFO - GET /rest/api/2/field asObject Request Duration: 1002ms
2021-01-18 15:24:29.592 INFO - PUT /rest/api/2/issue/431783 asString Request Duration: 443ms
2021-01-18 15:24:29.593 WARN - PUT request to /rest/api/2/issue/431783 returned an error code: status: 400 - Bad Request
body: {"errorMessages":[],"errors":{"customfield_11911":"Field 'customfield_11911' cannot be set. It is not on the appropriate screen, or unknown."}}
2021-01-18 15:24:29.594 INFO - Serializing object into 'interface java.util.Map'
2021-01-18 15:24:29.609 ERROR - Please use the ScriptRunner user to complete this task not the Initiating User.
See https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-editIssue for more information.
If you are using the ScriptRunner user then check the Field Configuration{code:xml}


How we can fix this while passing two custom fields to clear their values.

Thanks,
Sures2021-01-18 23_02_05-Script Listeners-Jira.pngConfig-01.pngHistory.pngScript.png

Kristian Walker _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.
January 18, 2021

Hi Suresh,

I can confirm that your error shows that the custom field with the ID of customfield_11911 is not on the edit screen and due to this the value cannot be cleared as the plugin can only clear values when the field exists on the edit screen.

As the error says to make the script work for fields not on the edit screen you need to uncomment line 11 and then change the script listener to run as the ScriptRunner add on User as this will then allow it to set the value of fields which are not on the edit screen.

Regards,

Kristian

Suresh Bhandiwad January 19, 2021

Hi Kristian,

As advised, I uncommented line 11 & choosed Add-On user. I am not finding the way to give add-on user required permission.

{"errorMaessages":["Only Connect add-on users with admin scope permission are allowed to override screen security."],"errors":{}}

How we can give this permission to ScriptRunner Add-On User? 

I do not find a way to do at global level. Can you please guide me?

Thanks,

Suresh2021-01-19 19_35_21-Script Listeners - Jira.png

Kristian Walker _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.
January 19, 2021

Hi Suresh,

Can you please confirm that you saved the script with line 11 uncommented and the As this User box set to the ScriptRunner Add-On User.

After this all new events which trigger the listener should then be able to update the field which is not on the screen. 

If this does not work then can you please provide a screenshot showing how you have configured the script to add in these changes.

Regards,

Kristian

Suresh Bhandiwad January 19, 2021

Hi Kristian,

Seriously, I am not sure what went wrong. Just saved the content once again & it worked.

Thanks,
Suresh

Kristian Walker _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.
January 19, 2021

Hi Suresh,

You are welcome and I am glad that your script is now working as expected.

Have a wonderful the rest of your day,

Kristian

Like Suresh Bhandiwad likes this
Sergei Gridnevskii
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 2, 2021

I don't think this solution is good.

 

1. It requires a paid plugin while Jira cloud already has Automation for Jira

2. Clones link can be added both by Jira and by a human. E.g. developer starts working on issue and finds out that it is a clone of another issue. He puts a link and some fields are cleared. That's not what he expects.

Andy Ukasick July 7, 2022

I agree.  What is needed is a way to set a post-process on the Create transition in the workflow which will only execute if the issue being created is a clone.  That sounds simple enough, but thus far I cannot find a way to do it.  I thought I could simply add a groovy script condition to a 'clear fields' post process such that the fields are only cleared if there is a 'clones' link present.  I thought that looking for the 'Clones' link was a reasonable way to tell if it's a cone.  Sounds good right?  It was easy enough to create the conditional post process, and it works if you test it on an issue that already has a clones link.  But when trying to use it in the create transition, I eventually came to realize that Atlassian only adds the clone link after the issue is created.  So I though I'd move the 'clear fields' post process down to after the issue is created and indexed.  Guess what, the link still isn't present yet.  So I thought... ok, I'll create another transition and put the 'clear fields' post process in another transition, then make the create transition auto-trigger the second transition after it completes.  But even though the second transition will clear the fields if executed manually, it does not do it if triggered automatically.  There are no permissions issues.  It seems that Jira then waits until after the second transition completes before finally adding the link that I'm trying to detect and use as a condition for clearing the fields.  In the issue History, the link being added is always the last thing that happens, even if I subsequently auto-trigger a third transition. It's really rather maddening.  Did they come up with this idea while swilling some good Aussie beer one night and lay bets over who could come up with the most impossibly annoying means and timing for adding the clones link to a cloned issue??  Honestly... is there no way to do this!?  Is their any other condition that could be checked to determine if the issue being created is a clone??  This is nuts.  Why is such a simple thing so hard!?

Like Nick Sievert likes this
2 votes
Nick Sievert September 8, 2022

@Andy Ukasick 

 

You could accomplish this with an automation rule: 

 

Trigger = When: Issue Created

Condition = If: Issue matches JQL (issueLinkType = Cloners)

Action = Then: Edit issue fields

2 votes
Suresh Bhandiwad May 23, 2022

Hi @Kristian Walker _Adaptavist_ 

Custom field also gets cleared when a user links issues by mistake or accidental using link type "Cloners". Is there a way to avoid this field getting cleared when an issues are linked manually using 'Cloners' link type instead Jira built in function?

Can we differenciate and tweak script to clear field when issue is cloned using link type 'Cloners'?

Thanks!

Nick Sievert September 8, 2022

@Suresh Bhandiwad try an automation rule instead: 

 

Trigger = When: Issue Created

Condition = If: Issue matches JQL (issueLinkType = Cloners)

Action = Then: Edit issue fields

Since this only runs at ticket creation there's no risk of fields getting cleared in the scenario you described.

Like Dan DuBois likes this
0 votes
Laci June 30, 2021

@Kristian Walker _Adaptavist_ Could this be used with a Jira System field such as Fix Version? Thanks!

0 votes
Suresh Bhandiwad September 16, 2019

By the way Field is Single line text, custom field.
Environment is Jira cloud

kedar pan January 26, 2022

@Kristian Walker _Adaptavist_ i tried running the script that you had but facing the code stat error. I also checked IssueLink Created event. 

Suggest an answer

Log in or Sign up to answer