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
4,295,759
Community Members
 
Community Events
165
Community Groups

Validating Attachments Added this Transition

I have a requirement to validate if attachments were added during a transition screen where the attachment field exists. I have followed the code Ref: https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/validate-attachments-links-in-transition.html#_validating_attachments_added_this_transition but that's not working. Can someone please confirm if that works even or if there are any caveats while using that.

4 answers

1 accepted

2 votes
Answer accepted

I have worked around the above issue as below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor

def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)
def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()

def attachmentNames = issue.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValue

if(attachmentNames) {
return true

Thanks - this helped solve the same problem for me, after a different approach stopped working after a Jira upgrade.

I had to modify the code to the below, but thanks for pointing me in the right direction...

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor
import com.opensymphony.workflow.InvalidInputException

def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)
def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()

def mIssue = issue as MutableIssue

def attachmentNames = mIssue.getModifiedFields().get(IssueFieldConstants.ATTACHMENT)?.newValue

if(!attachmentNames) {
invalidInputException = new InvalidInputException("You must add an attachment")
}
Like # people like this

Thanks! Seems like this is the only way to parse attachments added through the Jira Service Management Portal request creation!

Hi, is there any way to validate the newly created attachment names against a string?

Something like;

ComponentAccessor.attachmentManager.getAttachments(issue).any{it.filename.contains("teststring")}

but one that would also search through the attachments created on the screen? 

Sorry for the premature question, i managed to do it with;

def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)
StringBuffer sb = new StringBuffer()
if (formToken) {
def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
tempWebAttachments.each { TemporaryWebAttachment it ->
sb << it.filename
}
}

 

Guys,

I wish to validate an attachment on issue transition as well but I am new to script runner.

Can anyone guide me step by step what to do?

Do I create a script listener or what should I do in script runner to make this code run properly?

 

Thanks,

This tutorial is helpful: https://scriptrunner.adaptavist.com/latest/jira/tutorials/scripted-validators-tutorial.html

But what I did was edit the workflow, and go to the transition where I wanted to ensure an attachment was added. In my case this is the "create" transition going into the first status.

Add a Validator to the transition and choose "Script Validator [ScriptRunner]". Then choose "Custom script validator".

Gave the validator a name so that its purpose is clear later, and typed in the code that's in my comment above. See below:

Untitled.png

Then test the hell out of it.

Neil,

It looks you are on Jira Server. I am not sure Jira cloud works the same way and I certainly don't know how to translate your code into the cloud params.

If and when I solve it I'll paste my code here for cloud users reference.

Thanks for taking the time to reply here

Hello, 

What are your JIRA and ScriptRunner versions?

Jenna

Hi Jenna,

Jira is 7.3 while Scriptrunner is 5.1.6

 

Thanks,

Madhu

Suggest an answer

Log in or Sign up to answer
TAGS

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