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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Scriptrunner: Require Attachment on transition

Edited

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

1 vote
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.
Jul 26, 2018 • edited

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.

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.
Jul 26, 2018

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

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 Anna Hummel likes 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.
Jul 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 :)

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