Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

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

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

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?

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
TAGS
Community showcase
Published in Confluence

Introducing External Collaboration for Confluence

We’re excited to introduce external collaboration for Confluence, now available in early access. It is available to preview for Confluence Cloud Premium and Enterprise customers. (If you're not on ...

188 views 0 7
Read article

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