Add behavior script to give a warning after users select the end date less than start date

Kay January 17, 2020

Hello Jira Community, 

Can you please help edit code below? 

Purpose:

I have a Jira project that when creating the ticket I can set the Start Date and End date and throw an error when the End date is smaller than the Start date. I can achieve this task by using the workflow validator. However, after the ticket has been created. I can directly edit the Start Date and End Date and make the later smaller than the former. It looks like the workflow validator only works when users create tickets for the first time or when we edit the ticket. I want to use Behavior in Jira to throw users an error when they directly edit the End Date which is smaller than the Start Date or select the Start Date which is bigger than End Date. 

Problems:

- In the code below, because I choose the "Anticipated End Date and Time" as a field to edit, after firing this code below, when clicking on Anticipated End Date and Time, it open the ticket for me to edit.

- The code below doesn't give me any warning at all when directly change the End Date to be smaller than Start date. 

I highly appreciate all your kind feedback and answers. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.web.action.util.CalendarResourceIncluder
import com.atlassian.jira.component.ComponentAccessor
import groovy.transform.BaseScript
import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.core.util.StringUtils

@BaseScript FieldBehaviours fieldBehaviours

def optionsManager = ComponentAccessor.getOptionsManager()

// Get the Custom Field Manager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def startDateField = getFieldByName("Anticipated Start Date and Time")
def startDateValue = startDateField.getValue() as Date
def dueDateField = getFieldByName("Anticipated End Date and Time")
def dueDateValue = dueDateField.getValue() as Date
Calendar calendar = Calendar.getInstance(TimeZone.getDefault())
log.warn "starDateValue ${startDateValue}"

calendar.setTime(startDateValue)
int startDateDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)
log.warn "starDateValueweek ${startDateDayOfWeek}"
calendar.setTime(dueDateValue)
int dueDateDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)


// validation due date
if (dueDateValue != null ){
if(startDateValue > dueDateValue){
def invalidInputException = new InvalidInputException("Due Date must be greater than Start Date")
}
}


// validation start date
if (startDateValue != null){
if(startDateValue > dueDateValue){
def invalidInputException = new InvalidInputException("Due Date must be greater than Start Date")
}
}

 

 

1 answer

1 accepted

1 vote
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 20, 2020

Hi @Kay 

Behaviours don't work in InvalidInputExceptions().

Instead, you have to use the formField.setError(errorText) method.

You will need to make sure the same script is present as server-side script on each date field as changing either date fields should trigger the script to validate the input.  

Try something like this (untested)

import com.onresolve.jira.groovy.user.FieldBehaviours
@BaseScript FieldBehaviours fieldBehaviours

def startDateField = getFieldByName("Anticipated Start Date and Time")
def startDateValue = startDateField.getValue() as Date
def dueDateField = getFieldByName("Anticipated End Date and Time")
def dueDateValue = dueDateField.getValue() as Date

startDateField.clearError()
dueDateField.clearError()
strErrorText = "Due Date must be greater than Start Date"

if (dueDateValue != null && startDateValue != null ){
if(startDateValue > dueDateValue){
startDateField.setError(strErrorText)
dueDateField.setError(strErrorText)
}
}
Kay January 21, 2020

@Peter-Dave Sheehan 

Thank you for your answer. However, after creating the ticket, I still can directly edit the start date to be bigger than the end date. It doesn't throw me any error under the two fields below. When clicking on "Anticipated End Date and Time" below, it will bring me to edit the ticket page. I can't edit the date directly like "Anticipated Start Date and Time". I think this happens because I choose "Anticipated End Date and Time" as the field in Behavior. Are there any ways that we can enable the in-line edit for both fields after creating the ticket and the error will be thrown under the two fields below rather than in the edit ticket page?

Jira-Start Date.png

Kay January 21, 2020

Update:

I can do the inline edit for "Anticipated End Date and Time" now by disabling "Behavior inline edit issue assets" in Jira Manage Add-ons. However, I still can't get the error when selecting the start date greater than the end date. I would like to see the error under "Anticipated Start Date and Time" or "Anticipated End Date and Time" when I do the inline edit on either one, not on the edit ticket page. Thank you. 

Kaneka, 

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 21, 2020

If you enable in-line edit, the behaviour code will not run at all and you will not be able to prevent users from violating your rules.

Behaviour is only capable of running within the context of a create/edit/transition screen and prevent the submission before the change is saved. There is no "validator" concept for processing simple edits. It might be possible to use event listeners, but I'm not sure if you cancel the transaction. The data is stored and committed by the time the event fires. At best, you could reverse the change using code and somehow alert the users, but that would be a pretty bad user experience.

I have implemented the suggested code in my test environment. And it's working as described:

2020-01-21 10_50_10-Behaviours - JIRA QAD Inc. - Corporate Issue Tracker - https___projects-gcp.qad..png2020-01-21 10_49_03-Edit Issue _ JSP-4362 - JIRA QAD Inc. - Corporate Issue Tracker - https___projec.png

Kay January 28, 2020

@Peter-Dave Sheehan Thank you Peter for your detailed answer. 

Kaneka, 

G Sunil Kumar April 13, 2020

Hi,

 

I want to compare the time of a custom field with the present time. That is, I have a custom field name Start Date where the value i select should not be less than the present time. 

 

Regards,

Sunil

Mickaël Orsolino May 12, 2020

Hi @Peter-Dave Sheehan , 

 

Thank you for your help It's work fine.

Suggest an answer

Log in or Sign up to answer