Scriptrunner: Require Attachment on transition

Ryot July 23, 2018

I'm trying to use the following recipe from adaptavist but it just tells gives me an error no matter if I have an attachment or not.

https://scriptrunner.adaptavist.com/4.3.4/jira/recipes/workflow/validators/validate-attachments-links-in-transition.html#_validating_attachments_added_this_transition

I'm running JIRA 7.9.0, scriptrunner 5.3.16, and applying it to the "create" transition in my workflow in the validators section.

Any ideas whats going wrong? Is the recipe broken? Am I doing something wrong? Is there some customizing of the script I must do for my jira instance/project I'm missing?

Appreciate any help.

2 answers

1 accepted

2 votes
Answer accepted
Mark Markov
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.
July 26, 2018

Hello @Ryot

In scriptrunner example validator just logs filenames

If you want make attachments mandatory, try this code

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

def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)

if (formToken) {
def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
tempWebAttachments.each { TemporaryWebAttachment it ->
log.error("Uploaded attachment name: ${it.filename}")
}
if(!tempWebAttachments){
throw new InvalidInputException(IssueFieldConstants.ATTACHMENT,
"You must upload attachment")
}
}

Here we checks if attachments exist and if not, throw an exception.

Ryot July 26, 2018

Hey @Mark Markov- thanks for the code! this is definitely closer where I need to be, however, it's giving my validator error still (not the one within in your code), and its giving the error regardless if I have an attachment or not.

Just to confirm we are on the same page... is this code appropriate for adding as a workflow validator for the create step?

I notice that with workflow validators, there is already a dedicated "Error Message" field to enter an error message (and thats the one thats showing up for me)... but I see your code also has an error message within it. Should I be putting this type of code somewhere else, like a listener? Or can it be modded to work as a validator.

Again, forgive my ignorance as I'm pretty new to this stuff. I've added a couple screenshots to hopefully clarify.

Screen Shot 2018-07-26 at 12.52.08 PM.pngScreen Shot 2018-07-26 at 12.52.31 PM.png

Mark Markov
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.
July 26, 2018

Place this code into custom script validator instead of simple script and it will work :)

Ryot July 26, 2018

D'OH. I knew it had to be some kind of small oversight like that.... I kept on editing code from my initial attempt, which was in simple script, so it didn't even occur to me to try the custom script area. 

Thanks so much for the help @Mark Markov - everything's working now.

For anyone else that stumbles across this I also have restricted it to a specific issue type that we need this on by doing the following...

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

def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)

if (formToken) {
def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)
tempWebAttachments.each { TemporaryWebAttachment it ->
log.error("Uploaded attachment name: ${it.filename}")
}
if ((!tempWebAttachments) && issue.issueType.name == "Your Issue Type Name Here"){
throw new InvalidInputException(IssueFieldConstants.ATTACHMENT,
"You must upload an attachment")
}
}
Like # people like this
Mark Markov
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.
July 26, 2018

You re welcome! If it helps you, please mark answer as accepted. So that, other people will be able to find this answer easily for similar questions :)

0 votes
jamila.augustine March 12, 2020

Hi, I placed the above code into custom script validator and it is not working. Anybody knows why?

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 12, 2020

Same here. This used to work in Jira 7.1.1/Scriptrunner 4.3.16, but doesn't work now we're on Jira 8.5.4/Scriptrunner 5.9.1-p5

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

Jamila - if it helps, this worked for me (based on some code found on this discussion 

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

Suggest an answer

Log in or Sign up to answer