Creating Issues (subtasks) in Workflow Post Function

Trying to create an new issue in a Jira 7.13 post function. The validateCreate() call is failing and the error/warning message collections are empty.

public class CreateSubTask extends AbstractJiraFunctionProvider
{
private static final Logger log = LoggerFactory.getLogger(CreateSubTask.class);

public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException {
    DateFormat df = new SimpleDateFormat("dd/MM/yy HH:mm:ss");
    Date dateobj = new Date();

    MutableIssue parentIssue = getIssue(transientVars);
    IssueService issueService = ComponentAccessor.getIssueService();
    IssueInputParameters inputParameters = issueService.newIssueInputParameters();
    inputParameters.setProjectId(parentIssue.getProjectId());
    inputParameters.setIssueTypeId("Task");
    inputParameters.setSummary("New issue summary");
    inputParameters.setDescription("");
    ApplicationUser user = parentIssue.getCreator();

    IssueService.CreateValidationResult validationResult = issueService.validateCreate(user, inputParameters);
    if (!validationResult.isValid())
    {
        String errors = String.join(" ", validationResult.getErrorCollection().getErrorMessages());
        String warnings = String.join(" ", validationResult.getWarningCollection().getWarnings());

        parentIssue.setDescription(df.format(dateobj) + " - Validation failed - " +
                errors + " - warnings - " + warnings);
        return;
    }
    IssueService.IssueResult result = issueService.create(user, validationResult);
}

}

Description field - 30/09/20 13:11:42 - Validation failed - warnings -

Any help would be appreciated…

Found it, I had specified an invalid Issue type. When I changeed it to this it worked…

    inputParameters.setIssueTypeId(parentIssue.getIssueTypeId());

You’re printing List<String> errorMessages but you’re ignoring Map<String, String> errors. See https://docs.atlassian.com/software/jira/docs/api/latest/com/atlassian/jira/util/ErrorCollection.html#getErrors--

I’ve run into this developer error numerous times as a Systems Administrator and it really makes it hell to debug when the developer does not log the entire validationResult.

Ok, now I see the error messages…

        StringBuilder messages = new StringBuilder();

        for (Map.Entry<String, String> entry : validationResult.getErrorCollection().getErrors().entrySet()) {
            messages.append(entry);
        }

01/10/20 09:38:21 - Validation failed - issuetype=The issue type selected is invalid.

1 Like

No worries, I’m currently working with a vendor on this exact same issue :smiley: Good luck!

1 Like