Need to mandate the Attachments field based on another field's selection.
Asked by Karl Samson:

Hi All, I have a requirement to mandate the Attachments field, upon a certain Drop-Down field selection. The code I current have does not seem to work. Any suggestions welcome!

import com.atlassian.jira.component.ComponentAccessor

// Get the field objects  
def dropdownField = customFieldManager.getCustomFieldObject("customfield_23803") //  
def attachmentField = getFieldById ("Attachment") //

// Get the value selected in the dropdown  
def dropdownValue = dropdownField.getValue()

// Specify the options that should make the attachment field mandatory  
def mandatoryOptions = \["Encrypted Email", "ForumPass"\] // List of all options that make the attachment mandatory

// Check if the selected value is in the list of mandatory options  
if (mandatoryOptions.contains(dropdownValue)) {  
attachmentField.setRequired(true)  
} else {  
attachmentField.setRequired(false)  
}


Answers:
author: Matt Parks
body: Are you doing this in a Scriptrunner behavior? Here's the code that I use inside a scripted validator (also from Scriptrunner) as part of a workflow transition.

Without doing any digging, it's possible that Attachment isn't really a field, but more of a constant, which is why the code works the way that it does.

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) as String

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

comments: author: Karl Samson
body: Hi Matt, I'm afraid the above code does not work for us. Please can you explain how we can replace your "Octopus Update" field, with our Dropdown field?

Below is where we have got to:-

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 MethodofSendingField = customFieldManager.getCustomFieldValue ()

def dropdownValue = issue.getCustomFieldValue(methodOfSendingField)

if (dropdownValue != "Encrypted DVD") {

return true;

}

if (dropdownValue == "Encrypted DVD" \&\& (numIssues.size() == 0 \|\| numIssues == null)) {

invalidInputException = new InvalidInputException("There must be at least one attachment.")

}


author: Matt Parks
body: I'm not sure what this line is supposed to be doing.

*def MethodofSendingField = customFieldManager.getCustomFieldValue ()*

If you want to get a custom field object, which you'll need to do in order to get the value from that object, you'll need the following:

*def octUpdateTypeField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Octopus Update Type").first()*

Then, when you want to get the value from the field, you'll use the following"

*def octUpdateValue = issue.getCustomFieldValue(octUpdateTypeField) as String*

I'm not sure that you need to cast the value as a String when you're getting a value from a select list, but I do it more to remind myself of what I'm doing in the code.

Note that this code works if you're running it inside a Scriptrunner listener or scripted validator (or something like that). If you're running the code inside a behavior, which is what I think you might be doing, it will be a little different. Then, in order to get the custom field object and a value from that object, you'll use the following code:

*def facReqTypeField = getFieldByName("Factory Request Type")*  
*def facReqTypeValue = facReqTypeField.getValue()*


Tags: data-center

jira-data-center

jira-server

server