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.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
}
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")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks! Seems like this is the only way to parse attachments added through the Jira Service Management Portal request creation!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello,
What are your JIRA and ScriptRunner versions?
Jenna
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Jenna,
Jira is 7.3 while Scriptrunner is 5.1.6
Thanks,
Madhu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.