How can I Restrict Custom Date field value on subtask based on Parent issue?

Inayat N May 7, 2021

Hi,

I have a custom date field called "Completion Date" on both standard issues and subtasks.  I do not want "Completion Date" on a subtask to be later that the "Completion Date" on it's parent issue.  How can I enforce this?  This should apply on when creating or editing the field.

For example, if IssueA Completion Date = 1-1-2021, then Completion Date on SubtaskA <= 1-1-2021.

Has anyone does this with Scriptrunner? Thanks.

1 answer

1 accepted

2 votes
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.
May 7, 2021

Create a behaviour configuration linked to your project and sub-task issue type.

Then add a server-side script on the Completion Date field.

import com.atlassian.jira.component.ComponentAccessor

def dateFormat = 'dd-MMM-yyy'
def dateFld = getFieldById(fieldChanged)
def dateValue = dateFld.value as Date
dateFld.clearError()
def parentIssueFld = getFieldById('parentIssueId')
if(parentIssueFld && parentIssueFld.value && dateValue){
def parentIssue = ComponentAccessor.issueManager.getIssueObject(parentIssueFld.value as Long)
def cf = ComponentAccessor.customFieldManager.getCustomFieldObject(dateFld.fieldId)
def parentDate = parentIssue.getCustomFieldValue(cf) as Date
if(parentDate && parentDate < dateValue){
dateFld.setError("Subtask Date (${dateValue.format(dateFormat)}) cannot be after the date of parent issue (${parentDate.format(dateFormat)})")
}
}
Inayat N May 10, 2021

Thanks @Peter-Dave Sheehan !  This works perfectly.   Just one question about the logic on this line:

 if(parentDate && parentDate < dateValue){
dateFld.setError("Subtask Date (${dateValue.format(dateFormat)}) cannot be after the date of parent issue (${parentDate.format(dateFormat)})")

Why does parentDate appear twice on this line?  Does this translate to if parentDate is present AND parentDate is less than Subtask Completion Date?

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.
May 10, 2021

That is shorthand for 

if(parentDate != null){
if(parentDate < DateValue){
//do something
}
}

with && the first term is evaluated ... if it is true, then the second term is evaluated.

This way, we don't try to compare a null date against a valid date. If the date is null, we exit the condition immediately.

Suggest an answer

Log in or Sign up to answer