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!
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.
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.")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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.