Hi,
I'm trying to validate the number of attachments in a transition. You can get temporary attachments through form token or id temporary attachment. I am trying to access temporary attachments via form token, because I haven't found a way to get ids temporary attachments. As a result, validation only works for Jira. If you make the transition through plugin Actions for JIRA Service Desk from Customer Portal - validation fails, because form token is null and I can't get access to temporary attachments. How do I solve the attachment validation problem by number on Customer Poral page? How can I get form token?
import com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.issue.IssueFieldConstantsimport com.atlassian.jira.issue.attachment.TemporaryWebAttachmentimport com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManagerimport webwork.action.ActionContextimport com.opensymphony.workflow.InvalidInputExceptionimport org.slf4j.LoggerFactoryimport org.slf4j.LoggerLogger log = LoggerFactory.getLogger(this.getClass())def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)if (formToken) { def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken) int attachmentsSize = tempWebAttachments.size() log.warn('size {}', attachmentsSize) if (attachmentsSize > 1) { throw new InvalidInputException(IssueFieldConstants.ATTACHMENT, "You can't attach more than 1 document") } return true} else { log.warn('form token is null')}
Sources:
Cheers, Bogdan
Hi, @Toolstrek, LLC
You need to use custom REST Edpoint (Scriptrunner):
For example, this code will allow the transition if the number of attachments is greater than 1:
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate import groovy.json.JsonBuilder import groovy.transform.BaseScript import org.codehaus.jackson.map.ObjectMapper import javax.ws.rs.core.MultivaluedMap import javax.ws.rs.core.Response @BaseScript CustomEndpointDelegate delegate validate(httpMethod: "POST") { MultivaluedMap queryParams, String body -> def mapper = new ObjectMapper() def tree = mapper.readTree(body) def fieldValue = tree.get("Attachment") if(fieldValue != null ) { def attachmentDetails = fieldValue.collect(){ it.getFieldNames() } if(attachmentDetails.size() > 1){ return Response.ok().build() } } return Response.status(400).entity("Less than one attachment ").build() }
You are required to create a REST Endpoint in the script runner, as shown in the screenshot below:
2. After the REST Endpoint is created, you will use the link mentioned in the documentation and configure it accordingly, as shown in the screenshot below:Cheers, Bogdan!
Hi @Toolstrek, LLC
A similar answer is available in this Adaptavist Library link.
The sample code provided uses the validator to restrict the number of attachments that can be added. If it exceeds the limit, the validator will return an error message.
I hope this helps to answer your question. Thank you and Kind regards,
Ram
Hi, @Ram Kumar Aravindakshan _Adaptavist_
Thanks for your answer, but it still doesn't work for Customer Portal.
Could you please try to modify the code provided in the Adaptavist Library to this:-
import com.atlassian.jira.issue.IssueFieldConstantsimport com.atlassian.jira.issue.MutableIssuedef issue2 = issue as MutableIssuedef newAttachmentNames = issue2.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValue as List!(newAttachmentNames.size() > 5)
Below is a screenshot of the validator configuration:-
I have added this validator to the create transition, tested it in my environment, and it works as expected.
Below are a few test screenshots:-
1) First, when I try to create a technical support ticket, I try to enter 7 attachments. The maximum number of attachments has been set to 5. As expected, when I try to create the issue, the validator returns an error message.
2) When I remove the attachments to the max limit of 5, I can now create the ticket as shown below:-
Hi, @Ram Kumar Aravindakshan _Adaptavist_ Plugin Actions for Jira Service Desk allows you to make transitions on a customer portal.
But but the validation of attachments on these transitions for some reason still does not work.
It looks like you're new here. Sign in or register to get started.