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.
I have worked around the above issue as below:
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.IssueFieldConstantsimport com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessordef attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()def attachmentNames = issue.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValueif(attachmentNames) { return true}
Hello,
What are your JIRA and ScriptRunner versions?
Jenna
Hi Jenna,
Jira is 7.3 while Scriptrunner is 5.1.6
Thanks,
Madhu
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.ComponentAccessorimport com.atlassian.jira.issue.IssueFieldConstantsimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessorimport com.opensymphony.workflow.InvalidInputExceptiondef attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()def mIssue = issue as MutableIssuedef attachmentNames = mIssue.getModifiedFields().get(IssueFieldConstants.ATTACHMENT)?.newValueif(!attachmentNames) {invalidInputException = new InvalidInputException("You must add an attachment")}
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?
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:
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
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}}
It looks like you're new here. Sign in or register to get started.