Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

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
Published in Apps & Integrations

🍂📹 Apptoberfest demo contest roundup: vote for your favorite demos!

Hi Community! The submissions are in (and listed below) for the 🍻🍂Apptoberfest🍂🍻 Demo Competition and it’s time for you to place your votes for the best: Analytics & reporting app demo ...

122 views 2 11
Read article

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