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


I am writing behavior to restrict it with following 2 things.

  1) On Create subTask due date can not be before Parent issue Creation date and not to be after Parent issue due date.

2) Users should not allow on Edit subTask due date can not be before Parent creation date and after Parent due date. 

My code is as follow. Its working fine but setHelpText or setError is not showing when comparing SubTask Due date < Parent creation Date. Whats wrong with my code.


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue
import java.sql.Timestamp
import java.lang.Object

import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.web.util.OutlookDate
import com.atlassian.jira.web.util.OutlookDateManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript

import com.atlassian.jira.issue.CustomFieldManager
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.web.action.util.CalendarResourceIncluder
import static com.atlassian.jira.issue.IssueFieldConstants.*

import org.apache.log4j.Level

@BaseScript FieldBehaviours fieldBehaviours
def DueDate = getFieldById(getFieldChanged())
def DueSubtask = DueDate.getValue()
Date newDueSubtask = (Date)DueSubtask
def newDueSubtaskformat = newDueSubtask.format('MM/dd/yyyy')

def actName = getActionName()

if(actName != "Create"){
def issueId = formContents["id"]
def issue = ComponentAccessor.getIssueManager().getIssueObject(issueId as Long)
MutableIssue parentEdit = issue.getParentObject() as MutableIssue;

Date ReqDue = parentEdit.getDueDate()

if(newDueSubtask.compareTo(ReqDue)>0) {
def parduedateformat = ReqDue.format('MM/dd/yyyy')
// DueDate.setHelpText("Workitem due date ${newDueSubtaskformat} cannot be > Request due date ${parduedateformat}")
DueDate.setError("Workitem due date (${newDueSubtaskformat}) cannot be > Request due date (${parduedateformat})")
}else {

def parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long

def issueManager = ComponentAccessor.getIssueManager()
def parentIssue = issueManager.getIssueObject(parentIssueId)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

Date DueReq = parentIssue.getDueDate()
Date ReqCreatedate = parentIssue.getCreated()
log.warn "Parent create date" + ReqCreatedate

log.warn "I am in creation date comaprison ${newDueSubtask}"
def parentcreatedateformat = ReqCreatedate.format('MM/dd/yyyy')
DueDate.setError("Workitem due date cannot be < Request create date ")
// DueDate.setHelpText("Workitem due date cannot be < Request create date")
// DueDate.setFormValue("")
}else {
if((newDueSubtask.compareTo(DueReq)>0) ) {
def parduedateformat = DueReq.format('MM/dd/yyyy')
DueDate.setHelpText("Workitem due date (${newDueSubtaskformat}) cannot be > Request due date (${parduedateformat})")
} else {


 Even seeing the error in logs as below

 Cannot cast object '' with class 'java.lang.String' to class 'java.util.Date'

Any advise?


1 answer

1 accepted

0 votes
Answer accepted

It would have helped to know which line threw the error. That's usually available in the stack trace. 

But the following (simpler script) works for me:

import com.atlassian.jira.component.ComponentAccessor
def dateFormat = "MM/dd/YYYY"

if(issueContext.issueType.isSubTask()){ //only run behavior on sub-tasks
def dueDateFld = getFieldById(getFieldChanged())
def parent //placeholder parent issue object, we'll get it in the next step

if(getActionName() != 'Create'){
parent = issueContext.parentObject
} else {
parent = ComponentAccessor.issueManager.getIssueObject(formContents.parentIssueId.toLong())
if(parent){ //in case the parent object was not correctly identified somehow
if(parent.dueDate && dueDateFld.value){ //in case the parent or current due date is null
if(dueDateFld.value.toTimestamp() > parent.dueDate ){
dueDateFld.setError("Sub task Due Date (${dueDateFld.value.toTimestamp().format(dateFormat )}) cannot be > parent Due Date (${parent.dueDate.format(dateFormat )})")
if(dueDateFld.value.toTimestamp() < parent.created){
dueDateFld.setError("Sub task Due Date (${dueDateFld.value.toTimestamp().format(dateFormat )}) cannot be < parent Created Date (${parent.created.format(dateFormat )})")

@Peter-Dave_SheehanLook like parentObject method not finding. Does it work for you? I am getting error message as No such property.

   parent = issueContext.parentObject

Yes, you will see this in the scriptrunner script editor because issueContext is dynamic and the editor can't determine the class for sure. But it will work when the code runs (only when the issue exists).

I placed the script in behavior on due date field and saw below errors in field.

Static type checking errors are for information only. The true test is to look at the log when you run the code.

@Peter-Dave_SheehanLook like code works. I did see some error but due date validation works fine even the error shows in logs


2019-09-23 11:13:47,835 https-jsse-nio-8443-exec-66 ERROR jirauser 673x71127x1 e0vcp5 /rest/scriptrunner/behaviours/latest/validators.json [c.o.jira.behaviours.BehaviourManagerImpl] *************************************************************************************
2019-09-23 11:13:47,862 https-jsse-nio-8443-exec-66 ERROR jirauser 673x71127x1 e0vcp5 /rest/scriptrunner/behaviours/latest/validators.json [c.o.jira.behaviours.BehaviourManagerImpl] Script function failed on issue: issue: jproj-101, user: jirauser, fieldId: __init__, file: <inline script>
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(
at com.atlassian.jira.issue.Issue$getCustomFieldValue$ Source)

This shows that the error is on line 80 of your initialiser script. So not coming from what I gave you.

@Peter-Dave_SheehanIt is strange. I do not have anything on initialiser. Anyway our script runs fine for Due date. Thanks for the help.

Suggest an answer

Log in or Sign up to answer

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