Hello People,
Can anyone help me with the code 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 in advance,
Sahish
Hi @Sahish
You should create an event listener listening "IssueLinkCreatedEvent"
Here is the code that might boost your work
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
Long ISSUE_LINK_TYPE_ID = 10005 //Cloning ID//
String ISSUE_TYPE_ID = 10601 //Report ID//
IssueLink issueLink = event.getIssueLink()
if (issueLink.id == ISSUE_LINK_TYPE_ID) {
return
}
Issue sourceIssue = issueLink.getSourceObject()
Issue clonedIssue = issueLink.getDestinationObject()
if (clonedIssue.getIssueType().getId() != ISSUE_TYPE_ID) {
return
}
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue missue = (MutableIssue) clonedIssue
missue.setComponent(new ArrayList())
issueManager.updateIssue(loggedInUser, missue, EventDispatchOption.DO_NOT_DISPATCH, false)
I haven't tested the code, but I believe it would be a good start.
Hi @Tuncay Senturk ,
Thanks for the code. When I run this code, it got successful. But still I can see Component/s field in the cloned issue which has not become null. Still I can see the field in the cloned issue.
For us the field is like dropdown, it has so many Component/s to choose. May be this may cause anything for not becoming null ?
Thanks in advance,
Sahish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes null might not be the case there.
Instead, you may set an empty array of component.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Tuncay Senturk ,
Could you please help me on the setting empty array of component which shall show "None"?
Thanks in advance,
Sahish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Any help that you can provide on the setting empty array of component which shall show "None"?
Thanks in advance,
Sahish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just put empty ArrayList, here it is!
missue.setComponent(new ArrayList())
I updated the code above as well.
Hope it helps
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the code. Actually the code works fine but the problem is that the Component/s field gets Empty which is "None" in the original issue, not in cloned issue. But I want the component/s field to be Empty in the cloned issue not in original issue.
If ABC is a project, ABC-111 is a original issue, I am cloning from this issue and the issue is ABC-112. Now when I run the above code the components field is getting None in ABC-111 instead of ABC-112.
Is there anything we need to change the code properly to get the field "None"in the cloned issue rather than original issue?
Thanks,
Sahish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's interesting because the code should work to empty the destination issue.
As you can see these lines represent the source issue and cloned issue.
Issue sourceIssue = issueLink.getSourceObject()
Issue clonedIssue = issueLink.getDestinationObject()
And below clonedIssue's component is set.
MutableIssue missue = (MutableIssue) clonedIssue
missue.setComponent(new ArrayList())
issueManager.updateIssue(loggedInUser, missue, EventDispatchOption.DO_NOT_DISPATCH, false)
However, it might be because of the link type's direction (inward/outward)
Anyway, try changing the line with MutableIssue as below
MutableIssue missue = (MutableIssue) sourceIssue
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Here in our case we have seen that how to set empty ArrayList for system field. Need one more small help from you.
If I want to set an empty ArrayList to any custom field in the cloned issue, may I know how is that possible. (same scenario of above case)
The code which I used:
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
Long TEXT_CUSTOM_FIELD_ID = 16622
String ISSUE_TYPE_ID = 10211
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, new ArrayList())
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Thanks in advance,
Sahish.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It is based on which custom field type it is. If it is text custom field, you can use null.
issue.setCustomFieldValue(textCustomField, null)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I used null. If I use null, the custom field is not appearing in the cloned issue. It just disappeared. I want to have that field in my cloned issue and it should show "None" as the custom field is like dropdown, it has so many Component/s to choose.
Thanks
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Any help that you can provide me on the setting empty array for any custom field which shall show "None"?
Thanks in advance,
Sahish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, @Sahish
Sorry for the late response!
Jira's system field Component behaves differently than any other multi-select picker custom field. If you set a multi-select custom field with a null value or an empty list, it will automatically be disappeared.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
But if we use the below code line, even its not disappearing. Still I can see the field on the screen.
issue.setCustomFieldValue(textCustomField, new ArrayList())
May I know what could be the reason for this?
Thanks,
Sahish
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That's weird! Is it a multi-select custom field?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes its a multi-select custom field as same as System Field "Component/s".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I tested with a multi-select custom field and it disappeared from the screen when I set with an empty list (new ArrayList())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The following below code I have run, Could you please check whats going wrong in this?
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
Long ISSUE_LINK_ID = 10059
String ISSUE_TYPE_ID = 10651
Long TEXT_CUSTOM_FIELD_CompVer_ID = 15963
IssueLink issueLink = event.getIssueLink()
if (issueLink.id == ISSUE_LINK_ID) {
return
}
Issue clonesIssue = issueLink.getSourceObject()
Issue isClonedByIssue = issueLink.getDestinationObject()
if (isClonedByIssue.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_CompVer_ID)
MutableIssue missue = (MutableIssue) clonesIssue
missue.setCustomFieldValue(textCustomField, new ArrayList())
issueManager.updateIssue(loggedInUser, missue, 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.
Iam facing same issue can you please guid me...
Can this work be done in automation
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you for your message. It is possible to accomplish this through automation as well. However, if you need further details, I suggest posting a new question as this thread has become quite lengthy and may be difficult for community members to follow.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you please send the automation rule. How to write automation rule please post... Iam facing this issue since last month. Could you please provide the solution
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.