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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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 purge the "attachment" field?

Hi everybody,

 

I'm working on a project where I have 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 allowing the users to add a document to the subtask and with the help of a script the document is also added to the main ticket.

It's working fine.

The problem occurs if I re-open a subtask to which a document has been attached on the "close" transition. When I will close this subtask again, the attachment I added the first time will be added again in the main ticket. So I have the same document two times in the main JIRA. That's a big problem for my project.

So I tried to add a "clear field value" script on "attachment" field on the "close" transition (after the script that send the attachment to main JIRA of course) but it doesn't works.

I use the "clear field value" script on many transitions to reset te values of many fields and it works perfectly. The problem only concerns the "attachment" field. For example it works perfectly with the "assignee" field and "delivery date" field.

When I try to purge the ttachment field an error occurs and JIRA send a message advertising me that the transition is impossible to execute.
You can see the screenshot below:

Capture.PNG

I tried on different transition. Even on transition of the main ticket workflows. I still got the same error.

Do you have any idea on how I can "purge" the attachment field?

 

I'm using script runner 5.1.6 and JIRA 7.2.0

 

Thanks in advance!

 

Regards,

 

Germain.

1 answer

1 accepted

1 vote
Answer accepted
Ivan Tovbin Community Leader Feb 08, 2018

Hi Germain,

I think a slightly different approach will help you solve this. What if you just modify the script that is used to copy an attachment from a sub-task to its parent so that if there's already an attachment in the parent issue with the EXACT SAME name then the attachment from the sub-task will not be copied? Would that work for you?

Hi Ivan,

 

It's a great  idea but in my context it will not work as users will sometimes re open a subtask to modify what's inside the attachment but not the name. I need to allow them to send the same name attachment.

No problem, let's add a file size check, to be extra safe. Sure, it's not 100% bulletproof, but I have a feeling that a scenario where a file will have an EXACT size of the original after the edits is VERY unlikely.

That's another great idea. I will ask my management but I'm pretty sure they will tell me it's not a "safe" enough solution.

I keep you up to date.

Thanks!

In fact that's a solution that my management would accept!

 

So, Ivan, how would you write the script to compare name and size of the subtask's attachment and attachments already in the main ticket?

I want to allow the attachment to be added to main ticket if attachment's name is different of attachments in main ticket or if attachment's size is different if name is the same.

For information, here is the script I use to get the attachment (Yes, it's your code ^^):

def attMgr = ComponentAccessor.getAttachmentManager()
def attachments = attMgr.getAttachments(issue)
def parentAttachments = attMgr.getAttachments(parentIssue)
def lastAtt = attachments[0]
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
 
if (attachments.size() < 1){
  for (int i = 1; i < attachments.size(); i++){
    if (lastAtt.getCreated().getTime() < attachments[i].getCreated().getTime()){
      lastAtt = attachments[i]
    }
  }
}

attMgr.copyAttachment(lastAtt, currentUser, issue.getParentObject().getKey())

 

Thanks!

Ivan Tovbin Community Leader Feb 08, 2018

Right, let's get this show on the road, shall we? Try this:

import com.atlassian.jira.component.ComponentAccessor

def attMgr = ComponentAccessor.getAttachmentManager()
def attachments = attMgr.getAttachments(issue)
def parentAttachments = attMgr.getAttachments(issue.getParentObject())
def lastAtt = attachments[0]
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

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, currentUser, issue.getParentObject().getKey())
}

Ivan,

 

That's perfect, it works on first try!

It's a really good point for my project.

Thank you very much :)

 

Regards,

Suggest an answer

Log in or Sign up to answer
TAGS
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 ...

4,013 views 11 5
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