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.
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)})")
}
}
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.