How to invocate "validateTransition" method while issue is created from an incoming email?

Stefano De Gaetano
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 12, 2017

Hi all,

I've a question about a plugin that I am developing for my company.

In my jira environment, I've set an Incoming mail server and its relative mail handler.

When an email comes to a specific email address, an issue will be created under the project "XXX", issue type with id "1" (ticket).

I want that, if the email has a specific subject (for example "TEST"), the issue will be created and automatically closed with the comment "Closed automatically".

This is my plugin code:

 

public void execute(Map transientVars, Map args, PropertySet ps) throws InvalidInputException, WorkflowException{
		
   User adminUser = ComponentAccessor.getUserUtil().getUser("admin");
   ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(ComponentAccessor.getJiraAuthenticationContext().getUser());
		
   MutableIssue issue = (MutableIssue) transientVars.get("issue");
		
   if (issue.getKey().startsWith("XXX")) {
	if (issue.getIssueTypeId().equals("1")) {
	      if (issue.getSummary().contains("TEST")) {
					
		 List workflowEntries = ComponentAccessor.getOfBizDelegator().findByAnd("OSWorkflowEntry", FieldMap.build("id", issue.getWorkflowId()));
					
		 Iterator workflowEntriesIterator = workflowEntries.iterator();
		 while (workflowEntriesIterator.hasNext()) {
		 	GenericValue workflowEntry = (GenericValue) workflowEntriesIterator.next();
		        if (workflowEntry.getInteger("state") == null || "0".equals(workflowEntry.getInteger("state").toString())) {
			    workflowEntry.set("state", new Integer(WorkflowEntry.ACTIVATED));
			    try {
				workflowEntry.store();
			    } catch (GenericEntityException e) {
				e.printStackTrace();
			    }
		        }
		 }
		    		
		IssueService issueService = ComponentAccessor.getComponentOfType(IssueService.class);
		IssueInputParameters issueInputParameter = issueService.newIssueInputParameters();
		issueInputParameter.setComment("Closed automatically");
					
		ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(ComponentAccessor.getJiraAuthenticationContext().getUser());
			
		IssueService.TransitionValidationResult validationResult = issueService.validateTransition(adminUser, issue.getId(), 44, issueInputParameter);
					
		if (validationResult.isValid()) {
			issueService.transition(adminUser, validationResult);
		}
	      }
	}
   }
}

This plugin is a postfunction put on the workflow transition "Create", as last action (after the "fire the event")

If that type of issue, with that subject, is created by user interface, everithing works fine (issue created and automatically closed).

If this issue is created starting from the email, the issue will be created but not closed.

Anyone knows what's the problem?

Thanks in advance,

Stefano De Gaetano

1 answer

0 votes
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 15, 2022

Hi @Stefano De Gaetano ,

please check this article https://confluence.atlassian.com/jirakb/issue-created-through-email-does-not-have-the-expected-workflow-transitions-372605376.html

Are you sure that ticket created by email is of the correct type associated to your workflow?

Suggest an answer

Log in or Sign up to answer