Forums

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

Need to mandate the Attachments field based on another field's selection.

Karl Samson
Contributor
September 23, 2024

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

1 answer

0 votes
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.
September 23, 2024

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.")
}
Karl Samson
Contributor
September 25, 2024

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

}

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.
September 25, 2024

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()

 

Matthaus Lomberg
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 3, 2024

Hi Matt,

We've now tried various approaches to solve our outstanding issues with the script. Here is our latest attempt:-

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException

// Access the custom field manager and attachment manager
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def attachmentManager = ComponentAccessor.getAttachmentManager()

// Get the "Method of sending" custom field
def metOfSendingField = customFieldManager.getCustomFieldObjectByName("Method of sending")

// Get the value of the "Method of sending" field for the current issue
def metOfSendingValue = issue.getCustomFieldValue(metOfSendingField) as String

// Get the list of attachments for the issue
def attachments = attachmentManager.getAttachments(issue)

// Check if the "Method of sending" field value is "ForumPass"
if (metOfSendingValue == "ForumPass") {
// If no attachments, throw an exception to block the transition
if (attachments == null || attachments.isEmpty()) {
throw new InvalidInputException("You must add at least one attachment when 'ForumPass' is selected.")
}
}

The Logs gives us the following 

2024-10-03T11:54:57,872 ERROR [behaviours.BehaviourManagerImpl]: ************************************************************************************* 2024-10-03T11:54:57,872 ERROR [behaviours.BehaviourManagerImpl]: Script function failed on issue: (create issue) project/issuetype: RFA/Task, user: matt.lomberg, fieldId: customfield_23803, file: <inline script>, edit behaviour:groovy.lang.MissingPropertyException: No such property: issue for class: 7c2ac313eb71232b88ef568ebdb12f3c at 7c2ac313eb71232b88ef568ebdb12f3c.run(7c2ac313eb71232b88ef568ebdb12f3c.groovy:12) ~[?:?]

And here is the Payload

{ "undefined": "{null/empty} (java.lang.String)", "pid": "18400 (java.lang.String)", "atl_token": "B7OD-3ULM-DYVO-I92R_277df35d80317c6b23b842c9061620150b79c30d_lin (java.lang.String)", "formToken": "bf7bd7963e96e8ed37efffcd0b8b5748d3607d70 (java.lang.String)", "summary": "test (java.lang.String)", "reporter": "matt.lomberg (java.lang.String)", "assignee": "-1 (java.lang.String)", "customfield_23802": "{null/empty} (java.lang.String)", "customfield_23803": "27500 (java.lang.String)", "customfield_23813": "[-1] (java.util.ArrayList)", "customfield_23804": "{null/empty} (java.lang.String)", "customfield_23805": "{null/empty} (java.lang.String)", "customfield_23806": "{null/empty} (java.lang.String)", "customfield_23807": "-1 (java.lang.String)", "customfield_23808": "null (org.codehaus.groovy.runtime.NullObject)", "customfield_23809": "-1 (java.lang.String)", "customfield_23810": "{null/empty} (java.lang.String)", "customfield_23811": "{null/empty} (java.lang.String)", "customfield_23812": "{null/empty} (java.lang.String)", "dnd-dropzone": "{null/empty} (java.lang.String)", "issueTypeId": "10002 (java.lang.String)" }

 

Any further guidance would be greatly appreciated.

Many Thanks,

Matt. 

Suggest an answer

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

Atlassian Community Events