Custom Script Validator

SPATEL November 13, 2020

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

1 answer

1 accepted

1 vote
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 14, 2020

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?

SPATEL November 14, 2020

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. 

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 16, 2020

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.

SPATEL November 16, 2020

Hi @Hana Kučerová 

Thanks for this! As I need to validate every time, I can not use any custom Scripts on Validators (Workflow)?

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 17, 2020

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.

SPATEL November 19, 2020

Hi @Hana Kučerová 

Yeah, that's correct. Thanks a lot for all your help!

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2020

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!

Spatel November 27, 2020

Hi @Hana Kučerová 

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?

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2020

Hi @SPATEL and @Spatel :-),

thank you, I think you should be able to see green button "Accept answer" on this page, but only if you are logged in as a user, who has created this question. The problem could be, that you are using two different user accounts.

Suggest an answer

Log in or Sign up to answer