I created a single select custom field with 9 options. I am trying to clone Jira ticket to different projects based on those 9 options, is it possible to add a single post function to create transition?
I tried adding another post function to same create transition it gave me error that "at this time we are unable to create an issue for you, if the issue persists again please contact your administrator".
Hi,
1.Create a "Custom Listener" [available under Script Listeners]
2. Provide the project name for which you want to fire the event and select "Issue Created" event for events.
3. Here is the script [please read inline comments]. This script creates issue in different project based on custom field value. The new issue also set custom field value.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
//this is the custom field which I need to readh
def customFieldName = "MultiSelectA"
def customField = customFieldManager.getCustomFieldObjectByName(customFieldName)
// get the custom field value from the issue
def cfValue = event.issue.getCustomFieldValue(customField)?.get(0)?.value as String
//nothing to do as custom field value is not set, so return
if (!cfValue) return
//get the target project based on selected custom field option
def targetProject
if (cfValue == "AAA") {
targetProject = ComponentAccessor.getProjectManager().getProjectObjByKey("PROJ")
} else if (cfValue == "BBB") {
targetProject = ComponentAccessor.getProjectManager().getProjectObjByKey("PROJ2")
} else if (cfValue == "CCC")
targetProject = ComponentAccessor.getProjectManager().getProjectObjByKey("JRTWO")
//create a new issue
MutableIssue newIssue = issueFactory.getIssue()
newIssue.setProjectObject(targetProject)
//example of use ot setting summary from the property of the issue object
newIssue.setSummary("Created For ${event.issue.summary}")
newIssue.setDescription(newIssue.getDescription())
newIssue.setIssueType(targetProject.getIssueTypes().find { it.name == "Bug" })
//example of how to set custom Field value
def userCf = customFieldManager.getCustomFieldObjectByName("UserPicker")
newIssue.setCustomFieldValue(userCf, loggedInUser)
//now creating the issue
issueManager.createIssueObject(loggedInUser, newIssue)
log.debug "done ......."
Hi Ashraful Hasan,
Thank you for providing me the code. I am getting error at line 16, the custom field which I am using is a "Single list - Multiple choice "
Error Message: [Static type checking] Cannot find matching method java.lang.Object#get(int).
I really appreciate you for take time and providing me the solution.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is a static type checking error, it won't stop running the script. For an example in IDEA you wont get this error. Static type checking shows the error as "getCustomFieldValue" returns java Object and Object does not have get method. If you want to get rid of the error then you can replace the code with the following:
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption //add this new import
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def customFieldName = "MultiSelectA"
def customField = customFieldManager.getCustomFieldObjectByName(customFieldName)
def cfValueOptions = event.issue.getCustomFieldValue(customField) as List<LazyLoadedOption>
def cfValue = cfValueOptions.each {
it.getValue()
}
This code asks "getCustomFieldValue" function to get the value as a List of "LazyLoadedOption". Then it iterate over the list and create a list of String of the selected value and save it in "cfValue".
Lets say if you select 2 options from the list then you can access them by cfValue.get(0) and cfValue.get(1).
If nothing is selected then cfValue will be null and cfValue.get(0) will throw null pointer exception.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ashraful, the code you gave me is not working, but when I go to script listeners and check over there it says No failure in last 3 executions.
There are few required fields in source project do you thinks this causing the issue?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If there is no failure then it means the code is executing fine but it is not doing things of your expectation because it needs to be tailored according to your need. Seems to me you need someone who knows your environment and can change project keys and fields as necessary. Will you be able to do the modification?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
After reading the description here and in the issue in our portal it seems to me you can achieve this using Script Listener "Clones an issue, and links" or using a "Custom Listener" on "Issue Created" event.
What I mean is, let's say if you create an issue in project A then you can create an issue in different project with option based on specific field of project A. If this is the case then I think I should be able to help you. Feel free to add update here if this is not exactly what you are looking for.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah, that's right what I am looking for. I am using post function to clone a ticket with condition issue.getAsString('Customfiled_000') == 'Option B'
in this way I need to add atleast 15 postfunctions to the workflow, I don't want that.
And also is it possible to update a custom field based on reporter?
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.