Jira自定义Groovy脚本后置函数报错:缺少customFieldManager属性
Fixing the
MissingPropertyException in Jira 6.3.10 Groovy Post Function Script Let's break down your problem and get that subtask generation working properly. The error you're seeing (No such property: customFieldManager) is likely a side effect of two key issues in your script, plus some compatibility quirks with Jira 6.3.10.
Root Causes
- Null Issue Object: The error log clearly states
Script function failed on issue: null—your script is trying to use anissuevariable that hasn't been properly initialized. In Jira 6.x workflow scripts, you can't rely on an implicitissuevariable; you need to fetch it explicitly from the workflow context. - API Compatibility for CustomFieldManager: While
ComponentAccessor.getCustomFieldManager()exists in Jira 6.3.10, usingComponentAccessor.getComponent(CustomFieldManager.class)is more reliable for older Jira versions, as it avoids potential classloading issues that can lead to missing property errors.
Fixed Groovy Script
Here's the revised script that addresses both issues, plus adds error handling for robustness:
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.MutableIssue import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.bc.subtask.SubTaskManager import com.atlassian.jira.issue.IssueConstantManager // Fetch the current issue from workflow context (critical fix for null issue) def issue = transientVars.get("issue") as MutableIssue if (!issue) { log.error("No active issue found in workflow context—cannot generate subtasks") return } // Initialize managers using Jira 6.3.10-compatible methods def constantManager = ComponentAccessor.getConstantsManager() as IssueConstantManager def issueManager = ComponentAccessor.getIssueManager() def issueFactory = ComponentAccessor.getIssueFactory() def subTaskManager = ComponentAccessor.getComponent(SubTaskManager) def customFieldManager = ComponentAccessor.getComponent(CustomFieldManager) // List of custom field IDs (easily extendable for future fields) def personFieldsIds = [14098, 14099] for (def fieldId : personFieldsIds) { // Get the custom field object (add check to skip invalid IDs) def personField = customFieldManager.getCustomFieldObject(fieldId) if (!personField) { log.warn("Custom field with ID ${fieldId} not found—skipping subtask creation") continue } // Get the value from the custom field def assigneeValue = issue.getCustomFieldValue(personField) if (assigneeValue) { // Create new subtask MutableIssue newSubTask = issueFactory.getIssue() newSubTask.setReporter(issue.reporter) newSubTask.setAssignee(assigneeValue.getKey()) newSubTask.setSummary("Subtask for ${assigneeValue.getDisplayName()}") newSubTask.setParentObject(issue) newSubTask.setProjectObject(issue.getProjectObject()) // Fetch the Sub-task issue type (add check to avoid nulls) def subTaskIssueType = constantManager.getAllIssueTypeObjects().find { it.getName() == "Sub-task" } if (!subTaskIssueType) { log.error("'Sub-task' issue type not found in project—cannot create subtask for ${assigneeValue.getDisplayName()}") continue } newSubTask.setIssueTypeId(subTaskIssueType.id) newSubTask.setDescription("Description for ${assigneeValue.getDisplayName()}") // Create and link the subtask Map<String, Object> issueParams = ["issue": newSubTask] as Map<String, Object> def createdSubTask = issueManager.createIssueObject(issue.reporter, issueParams) if (createdSubTask) { subTaskManager.createSubTaskIssueLink(issue, createdSubTask, issue.reporter) log.info("Successfully created subtask for ${assigneeValue.getDisplayName()}") } else { log.error("Failed to create subtask for ${assigneeValue.getDisplayName()}") } } }
Key Improvements Explained
- Explicit Issue Fetch: We pull the issue from
transientVars(the standard workflow context storage in Jira 6.x) to ensure it's never null. - Reliable Manager Initialization: Using
ComponentAccessor.getComponent()forSubTaskManagerandCustomFieldManagerensures compatibility with Jira 6.3.10's classloading system. - Error Handling: Added checks for missing custom fields, missing issue types, and failed subtask creation, with log messages to help debug future issues.
- Extensibility: The
personFieldsIdslist is easy to update with new custom field IDs as needed.
How to Apply
- In your Jira workflow, edit the post function where you're using this script.
- Replace the existing script with the revised version above.
- Save the workflow and test creating a task with values in the custom fields 14098 and 14099.
内容的提问来源于stack exchange,提问作者Antonio
相关产品推荐
相关产品推荐

