How to validate the transition by the number of attachments on Customer Portal request?

Toolstrek, LLC August 14, 2022

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.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
import org.slf4j.LoggerFactory
import org.slf4j.Logger

Logger 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:

image.png

Cheers, Bogdan

2 answers

1 accepted

2 votes
Answer accepted
Bogdan Kozhukhov October 28, 2022

Hi, @Toolstrek, LLC 

You need to use custom REST Edpoint (Scriptrunner):

  1. Create custom REST Endpoint

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:

1.jpg

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:

2.jpg

Cheers, Bogdan!

2 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 14, 2022

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

Toolstrek, LLC August 16, 2022

Hi, @Ram Kumar Aravindakshan _Adaptavist_ 

Thanks for your answer, but it still doesn't work for Customer Portal.

Cheers, Bogdan

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 17, 2022

Hi @Toolstrek, LLC

Could you please try to modify the code provided in the Adaptavist Library to this:-

import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.MutableIssue

def issue2 = issue as MutableIssue

def newAttachmentNames = issue2.modifiedFields.get(IssueFieldConstants.ATTACHMENT)?.newValue as List

!(newAttachmentNames.size() > 5)

Below is a screenshot of the validator configuration:-

service_desk_validator.png

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.

image1.png

 

2) When I remove the attachments to the max limit of 5, I can now create the ticket as shown below:-image2.png

I hope this helps to answer your question. :)

Thank you and Kind regards,

Ram 

Like ZubatyNos likes this
Toolstrek, LLC August 22, 2022

Hi, @Ram Kumar Aravindakshan _Adaptavist_ 

Plugin Actions for Jira Service Desk allows you to make transitions on a customer portal.

image.png

image.pngBut but the validation of attachments on these transitions for some reason still does not work.

Cheers, Bogdan

Suggest an answer

Log in or Sign up to answer