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
Hi Sakshi,
Did you miss Jamie's comment on the 16th May?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi mark,
I see that last comment is mine on 23rd May. Is there anything that I miss?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sakshi,
I see that now (not easy when there are multiple answers and comments). No, you've not missed anything (I did)
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)?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I could guess what the problem might be, or you could just tell me?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
At the end:
if (issue.getCustomFieldValue(customField_select_list_name).toString().contains('Yes') && ! tempWebAttachments) { throw new InvalidInputException("attachment","Field is mandatory") }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.