Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Limit to one attachment on create transition

Larry Peery October 18, 2021

With a scriptrunner validator I can make the attachment required however I want to also limit the attachments to only one file. How can I get a count of attachments to use in the validator?

Scriptrunner doc
https://community.atlassian.com/t5/Jira-questions/Scriptrunner-Require-Attachment-on-transition/qaq-p/850615

Previous post on requiring an attachment
https://community.atlassian.com/t5/Jira-questions/Scriptrunner-Require-Attachment-on-transition/qaq-p/850615

Thanks!

1 answer

1 accepted

1 vote
Answer accepted
Alex
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.
October 18, 2021

Hi @Larry Peery 

If you could share your current script, it would be easier for the community to suggest changes. But based on the https://community.atlassian.com/t5/Jira-questions/Scriptrunner-Require-Attachment-on-transition/qaq-p/850615  post, I will suggest you something like:

if (tempWebAttachments.size() > 1){
throw new InvalidInputException(IssueFieldConstants.ATTACHMENT,
"You must upload only 1attachment")
}

I hope it helps. If this answer helps solve the problem, please come back and mark this answer as solved to help other community members with the same challenge. If not, you are welcome to share your solution as well.

Cheers,

Alex

Larry Peery October 18, 2021

Alex,

Thanks for the reply. I am using the script posted by Neil Arrowsmith (Thanks Neil!) referenced above.

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

 I'm wanting to add something like...

else if(attachmentNames.count("temp") > 1) {
invalidInputException = new InvalidInputException("Multiple attachments are not allowed. Only include 1 attachment.")
}

however, even though I know the returned value of "attachmentNames" is "[temp3931897663971913341, temp5383865475765667504]" I cannot use ".count" against "attachmentNames".

Alex
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.
October 18, 2021

@Larry Peery 

I might be missing something, but given that the attachmentNames variable has only the "temporary" attachment uploaded during the transition you might not need to count those with the name "temp". 

Based on your input the attachmentNames variable seems to be an array, therefore I"m pretty sure to can try the method attachmentNames.size() > 1 to validate the number of files uploaded during the transition.

Larry Peery October 18, 2021

That worked. Thanks!

Suggest an answer

Log in or Sign up to answer