Hello,
An issue has a multi-select custom field (checkboxes) with at least one selected option.
I'm trying to create a script for a workflow post-function that should do the following.
The easy part:
If the custom field has only one option selected, simply set issue type depending on the selected option and assign the issue to current user.
The tricky part (the one I could use some help with):
If the custom field has more than one option selected, then the issue's custom field should be updated to the first selected option, and cloned as many times as the number of remaining selected options. After that in each created clone the custom field should be set to only one of the remaining options respectively and clone's issue type updated according to that selection.
For example, the issue has the custom field with options 'a', 'b' and 'c' selected. After the post function is triggered, it should create two clones, each of which will have that custom field set to 'b and 'c' respectively. The original issue will have its custom field value set to 'a'. And all respective issues should have their issue types set according to custom field option selected. On paper it should look something like this:
Before cloning:
original issue (cf = a,b,c; issue type = whatever)
After cloning:
original issue (cf = a; issue type = 'Type A')
first clone (cf = b; issue type = 'Type B')
second clone (cf = c; issue type = 'Type C')
and so on
The code:
This is the code I came up with.
import com.atlassian.jira.component.ComponentAccessor
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(13801)
def fieldMgr = ComponentAccessor.getFieldManager()
def optsMgr = ComponentAccessor.getOptionsManager()
def issueSvc = ComponentAccessor.getIssueService()
def issueTypes = fieldMgr.getIssueTypeField().getOptionsForIssue(issue).getAt("id")
def svcList = optsMgr.getOptions(cf.getRelevantConfig(issue))
def svcIssueTypeMap = new LinkedHashMap()
def issueCf = new ArrayList()
for (i = 0; i < issue.getCustomFieldValue(cf).size(); i++){
issueCf.add(issue.getCustomFieldValue(cf)[i])
}
for (int i = 0; i < svcList.size(); i++){
svcIssueTypeMap.put(svcList[i],issueTypes[i])
}
if (issue.getCustomFieldValue(cf).size() > 1){
for (int i = 1; i < issueCf.size(); i++){
def cfValue = new ArrayList()
cfValue.add(issueCf[i])
issue.setCustomFieldValue(cf, cfValue)
issue.setIssueTypeId(svcIssueTypeMap.get(issueCf[i]))
def issueCfMap = new HashMap()
issueCfMap.put(cf,Optional.of(true))
def cloneVld = issueSvc.validateClone(currentUser, issue, issue.getSummary(), true, false, false, issueCfMap)
issueSvc.clone(currentUser, cloneVld)
}
}
def finalCfValue = new ArrayList()
finalCfValue.add(issueCf[0])
issue.setCustomFieldValue(cf, finalCfValue)
issue.setIssueTypeId(svcIssueTypeMap.get(issueCf[0]))
issue.setAssignee(currentUser)
As you can see, my idea is to set the values of the custom field and issue type in the current issue to the values that should be in the clone, then clone the issue, and repeat the process using the values for the next clone and so on. After all clones have been created I finally set the original issue's custom field and issue types the to values they should have.
Everything works just fine as long as the custom field has only one option selected (the easy part). When the custom field has multiple options selected this is what happens:
Before cloning:
original issue (cf = a,b,c; issue type = whatever)
After cloning:
original issue (cf = a; issue type = 'Type A') (good!)
first clone (cf = a,b,c; issue type = 'Type A')
second clone (cf = a,b,c; issue type = 'Type A')
Please advise, what am I doing wrong here?
Cheers,
Ivan