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 not clone a second time.
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.
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 = customFieldManager.getCustomFieldObjectByName("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)
}
Some more informations about that function and system:
- JIRA v8.4.1 Server
- Script Runner 5.6.2.1-jira8
Hi @Simon Krautwig,
You can ignore the warning sometimes, isn't first block working for you?
Try the below code
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)
BR,
Leo
Hi @Leo ,
thank you very much for your reply.
Currently I can see in the execution logs that the custom field "Clone" is set to "No" in the payload. If I reopen the issue and set it again to Done, the custom field remains on "No". See the screenshot.
I have replaced my code in "Additional issue actions" with your code and now get an error when executing it (now the issue is no longer cloned, no link created and no comment added that went before). The post-function log says following information:
[.....]
Caused by: com.atlassian.jira.workflow.WorkflowException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:778) at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:592) ... 317 more Caused by: java.lang.ClassCastException: com.atlassian.jira.issue.customfields.option.LazyLoadedOption cannot be cast to java.util.Collection at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39) at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693) at com.atlassian.jira.workflow.function.issue.IssueCreateFunction.execute(IssueCreateFunction.java:81) at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1014) at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1407) at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:606) at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:754) ... 318 more 2020-01-10 09:02:35,621 ERROR [workflow.AbstractScriptWorkflowFunction]: ************************************************************************************* 2020-01-10 09:02:35,621 ERROR [workflow.AbstractScriptWorkflowFunction]: Script function failed on issue: HAM-35, actionId: 21, file: null java.lang.NullPointerException: Cannot get property 'id' on null object at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue.doScript(CloneIssue.groovy:159) Start of logs truncated as they exceeded 300 lines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Simon Krautwig, I thought it was select list. but we need to pass collection for setting checklist field value
So the error says, it can't cast Option to Collection
can you change this to
issue.setCustomFieldValue(cf, [value])
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Leo
btw. You are Community leader. Would it be possible to change this Thread Tag from JIRA Software -> Question to "Marketplace Apps & Integrations-> Adaptavist" @Nic Brough -Adaptavist-
I changed it to
issue.setCustomFieldValue(cf, [value])
When I create the issue, everything works as before (clone, link and comment in the cloned issue works). Avoiding double cloning still doesn't work. If I open the ticket again and put it back on Done, it clones again every time. I attached a screenshot:
If it would help to serve another custom field better, I could also adapt it.
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)
}
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.
Published it in the right community thread. This can be closed here.
New thread: https://community.atlassian.com/t5/Adaptavist-questions/ScriptRunner-Clone-Issue-avoid-doubles/qaq-p/1268577#M5337
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.