Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,640,537
Community Members
 
Community Events
196
Community Groups

Make attachment mandatory at issue creation

Edited

Hello Atlassian heroes,

I would like to make attachments mandatory (under condition) at issue creation (Customer portal).

Here is my scenario : IF customfield1 = value1 AND customfield2 = value2 THEN attachment is mandatory.

To achieve this, I did create a "Simple Scripted Validator" at Create transition.

Here is my script :

import com.atlassian.jira.component.ComponentAccessor

def attachmentManager = ComponentAccessor.getAttachmentManager()
def hasAttachments = attachmentManager.getAttachments(issue).size() >= 1

if (cfValues['customfield1']?.value == "value1" && cfValues['customfield2']?.value == "value2" && !hasAttachments){
return false
} else {
return true
}

 I did also try this :

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.AttachmentManager;

def attachments = issue.getAttachments();

if (cfValues['customfield1']?.value == "value1" && cfValues['customfield2']?.value == "value2" && attachments.size() >= 1){
return false
} else {
return true
}

 But I got this kind of error :

Capture.PNG

Can you please help me achieve this ?

 

Jira Software version : 8.15.0

Jira Service Management version : 4.15.0

Script Runner version : 6.20.0

 

THANKS A LOT !

1 answer

1 accepted

2 votes
Answer accepted
Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 16, 2021

I think you can find the answer in this post. 

https://community.atlassian.com/t5/Jira-questions/Scriptrunner-Require-Attachment-on-transition/qaq-p/850615

You may need to modify the code slightly to add the condition to require the attachments only when your custom field(s) are in the specified state.

Thanks a lot !!!

Here is the code that worked for me :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.attachment.FileSystemAttachmentDirectoryAccessor
import com.opensymphony.workflow.InvalidInputException

def attachmentDirectoryAccessor = ComponentAccessor.getComponent(FileSystemAttachmentDirectoryAccessor)
def temporaryAttachmentDirectory = attachmentDirectoryAccessor.getTemporaryAttachmentDirectory()

def mIssue = issue as MutableIssue

def attachmentNames = mIssue.getModifiedFields().get(IssueFieldConstants.ATTACHMENT)?.newValue

if(!attachmentNames) {
invalidInputException = new InvalidInputException("You must add an attachment")
}
Like # people like this
Trudy Claspill
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Apr 19, 2021

Credit goes to @Mark Markov and Ryot for working out the code.

@Aimane SBAI I am curious, how did you wrap the code that you said works for you above into the logic you were using for initially checking against a custom field?  I have the same use case as you (or so I think), where if Custom Field A = Value B, then attachment needs to be required on create.  I read the code above, but can't seem to see where it's checking against a custom field value prior to executing the attachment logic.

Any input would be greatly appreciated!

Suggest an answer

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

Atlassian Community Events