Validating Attachments Added this Transition

Madhusudhan Matrubai December 19, 2017

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
Madhusudhan Matrubai December 19, 2017

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
Neil Arrowsmith
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 13, 2020

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
Nexign Team January 31, 2022

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

0 votes
Kaan Ozdemir April 7, 2022

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? 

Kaan Ozdemir April 7, 2022

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
}
}

 

0 votes
Hadas Hamerovv August 21, 2021

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,

Neil Arrowsmith
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 25, 2021

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.

Hadas Hamerovv September 2, 2021

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

0 votes
Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 19, 2017

Hello, 

What are your JIRA and ScriptRunner versions?

Jenna

Madhusudhan Matrubai December 19, 2017

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
AUG Leaders

Atlassian Community Events