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