It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

@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

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, 

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

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

Kaneka, 

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

Hi @Peter-Dave Sheehan , 

 

Thank you for your help It's work fine.

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you