Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner custom validator looking for specific field value on create screen

Matt Parks
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 11, 2023

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.")
}

 

2 answers

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
August 13, 2023

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

validator_config1.png

 

2. Next, select the Custom Script Validator [ScriptRunner] option as shown in the screenshot below:-

validator_config2.png

3. Once in the Custom Script Validator, add the sample code above as shown in the screenshot below:-

validator_config3.png

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.

test1.png

2. The issue is created as expected without any problem.

test2.png

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

test3.png

4. When I try to create the issue, as expected, the Validator returns an error message as shown in the screenshot below:-

test4.png

5. Next, I add some attachments and try to create the issue as shown in the screenshot below:-

test5.png

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

test6.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,
Ram

Matt Parks
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2023

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.

Like Vikrant Yadav likes this
0 votes
Vikrant Yadav
Community Champion
August 12, 2023

@Ram Kumar Aravindakshan _Adaptavist_  This is for you. Kindly help.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.1
TAGS
AUG Leaders

Atlassian Community Events