Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Inayat Nahvi
Contributor
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
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
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 Nahvi
Contributor
May 10, 2021

Thanks @PD 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?

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
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