Making Attachment Field Mandatory - custom Script Validator

sakshi Mittal May 13, 2016

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

0 votes
sakshi Mittal May 17, 2016

Any updates on this?

Regards

Sakshi

 

Mark McCormack (Adaptavist)
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.
May 27, 2016

Hi Sakshi,

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

sakshi Mittal May 27, 2016

 Hi mark,

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

Mark McCormack (Adaptavist)
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.
May 30, 2016

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

Development August 29, 2016

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 

0 votes
sakshi Mittal May 16, 2016

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

JamieA
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.
May 16, 2016

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

JamieA
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.
May 16, 2016

At the end:

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

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 

JamieA
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.
May 20, 2016

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.

sakshi Mittal May 23, 2016

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?

sakshi Mittal May 23, 2016

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

0 votes
JamieA
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.
May 15, 2016

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 

 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events