Making Attachment Field Mandatory - custom Script Validator

HI, 

I am writing a custom script validator to check the value of "Requires SQL"(Custom field) in a transition.

If the value is "yes" the attachment is mandatory else not.

Below is my script and it is working fine for all cases except when I actually attach an attachment and try to perform the transition. If attachment is present it should allow the transition to happen.

Could you please review the script? :

import org.apache.log4j.Category;
import com.atlassian.jira.ComponentManager;
import com.opensymphony.workflow.InvalidInputException;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.opensymphony.util.TextUtils
import com.atlassian.jira.component.ComponentAccessor;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField customField_select_list_name = customFieldManager.getCustomFieldObject("customfield_11407");

if (issue.getCustomFieldValue(customField_select_list_name).toString().contains('Yes'))
{
invalidInputException = new InvalidInputException("attachment","Field is mandatory")
}

 

Regards

Sakshi

 

3 answers

You are not actually doing anything there to make the attachment mandatory. You are just specifying that the error message appears on the Attachment field.

Validating that transitions have been added "this action" is not so easy... we have written a new doc page recently on this: https://scriptrunner.adaptavist.com/4.3.1-rc1/jira/recipes/workflow/validators/validate-attachments-links-in-transition.html 

 

Jaime,

Thanks for the link. I have modified my script but seems still there is a problem.

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.CommentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.util.ImportUtils;
import com.opensymphony.util.TextUtils
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.attachment.TemporaryWebAttachment
import com.atlassian.jira.issue.attachment.TemporaryWebAttachmentManager
import webwork.action.ActionContext

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField customField_select_list_name = customFieldManager.getCustomFieldObject("customfield_11407");
def temporaryAttachmentUtil = ComponentAccessor.getComponent(TemporaryWebAttachmentManager)
def formToken = ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN)
def tempWebAttachments = temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken)

//checking if Custom Field is "Yes" and Attachment is not Present

if (issue.getCustomFieldValue(customField_select_list_name).toString().contains('Yes') && tempWebAttachments == null)
{
invalidInputException = new InvalidInputException("attachment","Field is mandatory")
}

I could guess what the problem might be, or you could just tell me?

At the end:

if (issue.getCustomFieldValue(customField_select_list_name).toString().contains('Yes') && ! tempWebAttachments) 
{
  throw new InvalidInputException("attachment","Field is mandatory")
}

I already tried "! tempWebAttachments", was not working so I changed it to tempWebAttachments== null.

I am not sure what do you mean by "Guess" there. I am just trying to make this script work.

It would be good if you can test this at your end.


Regards

Sakshi 

Is it getting to the "throw" line? Add some logging to see... eg:

log.warn ("I am about to throw an exception..")

then check your logs.

Jaime,

I updated 

if (issue.getCustomFieldValue(customField_select_list_name).toString().contains('Yes') && !tempWebAttachments)
{
throw new InvalidInputException("attachment","Field is mandatory")
log.warn ("I am about to throw an exception..")
}

Its not getting into the throw line. The condition is resulting to be false?

I think it is not at all validating the attachment because if I add this code it should atleast throw the warning:

if (issue.getCustomFieldValue(customField_select_list_name).toString().contains('Yes') && !tempWebAttachments)
{
throw new InvalidInputException("attachment","Field is mandatory")

}
else{log.warn ("I am about to throw an exception..")}

In addition the script execution shows Failed

image2016-5-23 13:17:24.png

Any updates on this?

Regards

Sakshi

 

Hi Sakshi,

Did you miss Jamie's comment on the 16th May?

 Hi mark,

I see that last comment is mine on 23rd May. Is there anything that I miss?

Hi Sakshi,

I see that now (not easy when there are multiple answers and comments). No, you've not missed anything (I did) smile

Have you tried simpler combinations in the if statement to ensure you can get the exception to work (like just one of the if conditions)?

Hello all,

I am also using same method like

def formToken =ActionContext.getRequest()?.getParameter(IssueFieldConstants.FORM_TOKEN) 

But my formToken value is coming empty. 

Kindly reply me i am not getting any response about this question, and i dont know where i am going wrong.

Regards

Development 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 29, 2018 in Marketplace Apps

How to set up an incident workflow from the VP of Engineering at Sentry

Hey Atlassian community, I help lead engineering at Sentry, an open-source error-tracking and monitoring tool that integrates with Jira. We started using Jira Software Cloud internally last year, a...

1,326 views 0 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you