During the issue creation process, I want to force a user to attach at least one file if the value in a particular custom field has a specific value. However, during the validation, the issue hasn't been created yet, so the custom field doesn't technically have the value during the validation.
I'm using the Scriptrunner custom scripted validator. I am able to get it working if I don't care about the value in the custom field (i.e. it ALWAYS requires an attachment), but I can't figure out how to write the condition into the script.
Octopus Update Type is the custom field name and I want to require an attachment if the value of that field is Octopus Variable Update. Below is the script (I apologize in advance for the formatting, I don't remember how to make it a code block)
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.component.ComponentAccessor
def numIssues = ((MutableIssue) issue).getModifiedFields().get(IssueFieldConstants.ATTACHMENT).getNewValue() as Collection<Long>
def octUpdateTypeField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Octopus Update Type").first()
def octUpdateValue = issue.getCustomFieldValue(octUpdateTypeField)
if (octUpdateValue != "Octopus Variable Update") {
return true;
}
if (octUpdateValue == "Octopus Variable Update" && (numIssues.size() == 0 || numIssues == null)) {
invalidInputException = new InvalidInputException("There must be at least one attachment.")
}
Hi @Matt Parks
After reviewing your code, your validation doesn't work because your approach to verify if the Attachment is included is incorrect.
Invoking the Attachment field, as you have done below, will not work:-
def numIssues = ((MutableIssue) issue).getModifiedFields().get(IssueFieldConstants.ATTACHMENT).getNewValue() as Collection<Long>
Instead, you need to invoke the Attachment like this:-
import com.atlassian.jira.issue.fields.AttachmentSystemField
import webwork.action.ActionContext
def attachmentFiles = ActionContext.request?.getParameterValues(AttachmentSystemField.FILETOCONVERT)
Below is a full example of a working code for your reference:-
import com.atlassian.jira.issue.fields.AttachmentSystemField
import com.opensymphony.workflow.InvalidInputException
import webwork.action.ActionContext
// Rename the field according to your environment
def sampleTextFieldValue = issue.getCustomFieldValue('Sample Text Field')
def attachmentFiles = ActionContext.request?.getParameterValues(AttachmentSystemField.FILETOCONVERT)
if (sampleTextFieldValue && attachmentFiles == null) {
throw new InvalidInputException('attachments', 'If the text field has a value at least one attachment is included')
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below are the configuration screenshots for your reference:-
1. Go to the workflow and click on the Create transition. When the dialog appears, click on the Validator link as shown below:-
2. Next, select the Custom Script Validator [ScriptRunner] option as shown in the screenshot below:-
3. Once in the Custom Script Validator, add the sample code above as shown in the screenshot below:-
Below are a couple of test screenshots for your reference:-
1. For the 1st test, I will create an Issue without any value added to the Sample Text Field, as shown in the screenshot below.
2. The issue is created as expected without any problem.
3. For the 2nd test, I am going to create an issue with some value added to the Sample Text Field and will leave the Attachments empty shown in the screenshot below:-
4. When I try to create the issue, as expected, the Validator returns an error message as shown in the screenshot below:-
5. Next, I add some attachments and try to create the issue as shown in the screenshot below:-
6. As expected, the issue is created with the value set for the Sample Text Field and the Attachment included as shown in the screenshot below:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
Ram,
I tested your code and got the same issue. I realized the problem is that the Octopus Update Type field is a select list, so I needed to cast the value of that field as a String. Once I did that, your code (and my original code, for that matter) worked properly.
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.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.