It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner Clone Issue - avoid doubles Edited

Hi Community,

I am using ScriptRunner for the first time, have no previous programming experience and am working on using the function "Clones an Issue, and links". I added the function as a post function in the workflow when a defined status was reached.

The issue should be cloned into the second project, in addition a link to the source issue and a comment added with a defined text and by who the action was triggered.

These actions also work, but I am attached to the problem that if the issue has already been cloned once, a status change of the issue will clone it a second time. But if it has already been cloned once, it cannot be cloned a second time. I want to avoid duplicate clones.

For this I have created a CustomField, which I read out in the condition and, if necessary, set in the Additional issue action. I am stuck here and hope for your support.

I have already searched through some documentation, but have not yet come up with the right result.

Any idea what is wrong here?

 

On the screenshot you can see that whenever the ticket is opened and closed again, a clone is created several times and the custom field is not changed.

CollectionTest.pngcustomfield.png

 

 

The code is as following:

Condition: 

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;

//cfValues['Clone']?.value != "Yes"
//cfValues['Clone']*.value.contains("Yes")
if (cfValues['Clone']*.value.contains("Yes")) {
return false
} else {
return true
}

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
! changeHistoryManager.getAllChangeItems(issue).find {
it.field == "Done"
}

 

Additional issue actions:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()


def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Clone")
def fieldConfig = cf.getRelevantConfig(issue)
def value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Yes' }
issue.setCustomFieldValue(cf, [value])

//def cf = customFieldManager.getCustomFieldObjectsByName("Clone") // Checkboxes is the NAME of my custom field
//def fieldConfig = cf.getRelevantConfig(issue)
//def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)
//issue.setCustomFieldValue(cf, [option])
//issue.setCustomFieldValue(cf, "Yes")

//Kommentiert den Issue im Ziel Project, dass es sich um einen gecloned Issue handelt
doAfterCreate = {
def commentManager = ComponentAccessor.commentManager
commentManager.create(issue, currentUser, "This is a cloned issue from project HAM by ${currentUser.name}", true)
}

 

2 answers

1 accepted

0 votes
Answer accepted
Peter-Dave Sheehan Community Leader Monday

First, let's have a look at your condition:

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;

//cfValues['Clone']?.value != "Yes"
//cfValues['Clone']*.value.contains("Yes")
if (cfValues['Clone']*.value.contains("Yes")) {
return false
} else {
return true
}

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
! changeHistoryManager.getAllChangeItems(issue).find {
it.field == "Done"
}

I'm not sure what you intended to do with the changeHistoryManager, but since both clauses of the if() statement above include a "return" statement, the changeHistoryManager will never be executed.

In the Additional Issue Action code block "issue" refers to the new clone that is about to be created. So attempting to set the clone customfield value there will not work.

You could write additional code to change sourceIssue as part of Additional Issue Action, but it might be easier to just have a diffferent post function set your custom field value to yes.

But, reading your comment more carefully, I don't think you need the custom field. Your initial attempt to look at the changeHistory in the condition might work. Another option is to use the linkedIssueManager to look for an existing clone.

Try something like this in your condition

import com.atlassian.jira.component.ComponentAccessor;
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

!changeHistoryManager.getAllChangeItems(issue).any {
     it.field == "status" && it.tos*.value.contains("Done")
}

You may want to replace "Done" with the actual status name (not transition).

This means, only perform this post function if the current issue has never been in the "Done' state before.

Hi @Peter-Dave Sheehan .Thank you very much. I was able to solve the problem with your suggestion. That actually helped me a lot and the script was very short.

 

Greetings from Germany,

Simon! 

hi @Simon Krautwig 

not sure that I understood you completely, but it seems like the custom field update didn't store the value in the database.

try to add this line to your code to store the data:

ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Hi @Dar Kronenblum ,

thank you for your reply. But with the solution from @Peter-Dave Sheehan I was able to slim down the whole thing. Thank you very much anyway.

Best regards.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira Align

Announcing the Jira Align Early Advocate limited edition badge!

Hello and Happy New Year to our Jira Align Community Collection visitors! The Jira Align community is growing quickly, and we’re excited about all the great content that has been posted so far. To ...

1,356 views 41 27
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you