I'm trying to write a custom script on validator for following requirement
When User selects ABC component it makes due date mandatory (for other component it'll be there but not mandatory), and if user enters the duedate before 3 date from today then it'll throw an error(again only for ABC component) . I have written following script but it's not working. Any suggestions? Thanks in advance!
import com.atlassian.jira.component.ComponentAccessor
import java.sql.Timestamp
import com.opensymphony.workflow.InvalidInputException
import java.util.Date.*
def formComponent = getFieldByName("Component")
def dueDate = getFieldById("customfield_12204")
def createdDate = getFieldById("customfield_14364")
def cfval = cf.getValue() as Date
// get todays date
def today = new Date()
// get the date three months in the future
def threeDaysDate = today.plus(3)
//def today = new java.sql.Timestamp(new Date().getTime())
def componentFieldValues = formComponent.value
if (componentFieldValues*.name?.contains("ABC")){
dueDate.setHidden(false)
dueDate.setRequired(true)
if(cfval.before(threeDaysDate)) {
cf.setError("##ERROR##")
// if the date entered is over three months throw an error
} else{
cf.clearError()
}
}
//else {
//dueDate.setHidden(true)
//}
Hi @SPATEL
welcome to the Atlassian Community!
I will try to help you, but please, provide me more information.
Component/s field and Due Date field you are working with - are these the Jira System fields, or did you created them as a new custom fields? If so, which types do they have?
Hi @Hana Kučerová
Thanks!
Component/s and Due date both are System fields. I do have Custom Due Date as well of Date Picker type.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @SPATEL ,
please try something like this:
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.onresolve.jira.groovy.user.FormField
import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
import static com.atlassian.jira.issue.IssueFieldConstants.DUE_DATE
String componentName = "ABC"
FormField componentsField = getFieldById(COMPONENTS)
FormField dueDateField = getFieldById(DUE_DATE)
Collection<ProjectComponent> components = componentsField.getValue() as Collection<ProjectComponent>
Date dueDate = dueDateField.getValue() as Date
Date limitDate = new Date() + 3
dueDateField.setRequired(false)
dueDateField.clearError()
if (components*.getName()?.contains(componentName) ) {
dueDateField.setRequired(true)
if (dueDate.before(limitDate)) {
dueDateField.setError("##ERROR##")
}
}
You would need to add it as a server-side script for both Component/s and Due Date fields, because you would want to do the validation everytime one of the fields change.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Hana Kučerová
Thanks for this! As I need to validate every time, I can not use any custom Scripts on Validators (Workflow)?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @SPATEL
you can use them, but it won't be very nice solution - you will probably end up having validator on each transition and force users to edit the issue using transitions and screens on them (because you need to validate every time).
So, I believe, in this case is better to use behaviours.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yeah, that's correct. Thanks a lot for all your help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @SPATEL ,
if everything worked for you, would you please mark my answer as accepted? This action will mark the question as resolved and it can also help other users in the future to find the solution, if they have similar problem. Thank you!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It definitely helped me and other day I was looking for option to accept but couldn't find. Would you mind to show me, please?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.