Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

Validator avoiding copy of attachment if already existing

Hi everybody,


I'm working on a project with two workflows. one for the main ticket and one for the many subtasks that can be created during the main ticket resolution process.


When I close a subtask I have a screen with an attachment field.

Using the script below I can copy the attachment that is entered in the attachment field to the attachment field in the main ticket.


import com.atlassian.jira.component.ComponentAccessor

def cwdUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def attMgr = ComponentAccessor.getAttachmentManager()
def attachments = attMgr.getAttachments(issue)
def parentAttachments = attMgr.getAttachments(issue.getParentObject())
def lastAtt = attachments[0]
if (attachments.size() > 1){
  for (int i = 1; i < attachments.size(); i++){
    if (lastAtt.getCreated().getTime() < attachments[i].getCreated().getTime()){
      lastAtt = attachments[i]

boolean sameFileExists = false
if (parentAttachments.size() > 0){
  for (int i = 0; i < parentAttachments.size(); i++){
    if (lastAtt.getFilename() == parentAttachments[i].getFilename() && lastAtt.getFilesize() == parentAttachments[i].getFilesize()){
      sameFileExists = true
if (sameFileExists == false){
  attMgr.copyAttachment(lastAtt, cwdUser, issue.getParentObject().getKey())

The script is perfectly working.


Now, I would like a validator that will compare the attachment that is entered in the attachment field of the close subtask transition with the existing subtasks in the main ticket.  If the new attachment has the same name than one of the attachment in the main ticket I would like the validator to shoot a warning message and block the transition.


Do you have any idea on how to manage that?


Thanks in advance.





1 answer

Hi Germain,

This is totally doable actually. Please keep in mind a few things though:

1) The validator checks temporary attachments (files which are added on your screen but not yet uploaded to the issue) against attachments in other subtasks, and if a match is found (I've added not only a filename but also a filesize check to be extra safe) then an error message is thrown. 

2) To pass validation, simply removing the file you have attached on your screen is not enough, because the file will still remain in the action context and to clear the context you have to CLOSE and REOPEN your transition screen. Otherwise you'll still be getting validation errors even if you attach completely different files.

The code:

import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import com.atlassian.jira.issue.IssueFieldConstants
import webwork.action.ActionContext

def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)

if (formToken){
def tempAttMgr = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def tempWebAtts = tempAttMgr.getTemporaryWebAttachmentsByFormToken(formToken)
if (tempWebAtts){
def attMgr = ComponentAccessor.getAttachmentManager()
def subtasks = issue.getParentObject().getSubTaskObjects()
if (subtasks.size() > 1){
subtasks.remove(subtasks.find{it.getId() == issue.getId()})
for (int i = 0; i < tempWebAtts.size(); i++){
for (int v = 0; v < subtasks.size(); v++){
if (attMgr.getAttachments(subtasks[v]).find{it.getFilename() == tempWebAtts[i].getFilename() && it.getFilesize() == tempWebAtts[i].getSize()} != null){
throw new InvalidInputException ("File ${tempWebAtts[i].getFilename()} already exists in subtask ${subtasks[v].getKey()}".toString())

Hi Ivan,


Thanks for your answer! I was on a long WE. I will test your code this afternoon.


Many thanks.



Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

224 views 0 6
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