It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

0 answers

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

1,097 views 7 0
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you