I'm using the below script to create sub-tasks using Automation and working fine. The issue I'm facing here is that I'm unable to copy the value of Software Sub-Product relevant values based on the Software Products like like Adobe, Microsoft. Adobe has Software Sub-Products like Animate and Microsoft has Copilot. Each Sub-task is getting created for each Adobe and Microsoft. I want to copy Animate under Software Sub-Product field in the Adobe sub-task and Copilot under Software Sub-Product field for Microsoft Sub-task.
Both Software Product and Software Sub-Product Fields are Asset object field (multi-select).
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
// Define the IDs for the custom fields
Long softwareVendorFieldId = 31701L // Multi-select asset object field for "Software - Vendor"
Long installForFieldId = 32006L // Custom field ID for "Install For"
Long softwareSubProductFieldId = 31702L
// Retrieve the current issue from the context
def issueManager = ComponentAccessor.getIssueManager()
MutableIssue parentIssue = issueManager.getIssueObject(issue.id) // Ensure issue is available
// Fetch the custom field for "Software - Vendor" and "Install For"
CustomField softwareVendorField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(softwareVendorFieldId)
CustomField installForField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(installForFieldId)
CustomField softwareSubProductField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(softwareSubProductFieldId)
def selectedOptions = parentIssue.getCustomFieldValue(softwareVendorField) as List
def installForValues = parentIssue.getCustomFieldValue(installForField) as List
def softwareSubProductValues = parentIssue.getCustomFieldValue(softwareSubProductField) as List
if (selectedOptions) {
// Log each option to inspect its type and create a subtask for each
selectedOptions.each { option ->
log.info("Selected option: " + option.toString())
createSubTaskForOption(parentIssue, option, softwareVendorField, installForField, softwareSubProductField, installForValues, softwareSubProductValues)
}
}
void createSubTaskForOption(MutableIssue parentIssue, def option, CustomField softwareVendorField, CustomField installForField, CustomField softwareSubProductField, def installForValues, def softwareSubProductValues) {
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()
IssueManager issueManager = ComponentAccessor.getIssueManager()
// Create a new subtask instance
MutableIssue subTask = issueFactory.getIssue()
// Assuming option is a simple string (adjust as necessary based on logging)
subTask.setSummary("Software Request: " + option) // Subtask summary based on the option value
subTask.setIssueTypeId("22500") // Replace "5" with your subtask issue type ID
subTask.setParentObject(parentIssue)
subTask.setProjectObject(parentIssue.getProjectObject())
subTask.setReporter(parentIssue.getReporter())
// Copy the "Software - Vendor" field value (current option) to the subtask
subTask.setCustomFieldValue(softwareVendorField, [option]) // Only set the current option
// Copy the "Install For" field value to the subtask (copying all values from the parent)
if (installForValues) {
subTask.setCustomFieldValue(installForField, installForValues)
}
if (softwareSubProductValues) {
subTask.setCustomFieldValue(softwareSubProductField, softwareSubProductValues)
}
// Store the new subtask issue
issueManager.createIssueObject(user, subTask)
// Use SubTaskManager to link the subtask to the parent issue
def subTaskManager = ComponentAccessor.getSubTaskManager()
subTaskManager.createSubTaskIssueLink(parentIssue, subTask, user)
log.info("Subtask '${subTask.summary}' created and linked successfully.")
}
def filterRelevantSubProducts(def softwareSubProductValues, def option) {
return softwareSubProductValues.findAll { subProduct ->
subProduct.toString().contains(option.toString())
}
}
Thanks, in advance! Can anyone help?