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

Hi,

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
log.setLevel(Level.DEBUG)

@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 {
//DueDate.clearHelpText()
DueDate.clearError()
}
}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

if(newDueSubtask.compareTo(ReqCreatedate)<0){
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 {
//DueDate.clearHelpText()
DueDate.clearError()
}
if((newDueSubtask.compareTo(DueReq)>0) ) {
def parduedateformat = DueReq.format('MM/dd/yyyy')
DueDate.setHelpText("Workitem due date (${newDueSubtaskformat}) cannot be > Request due date (${parduedateformat})")
DueDate.setFormValue("")
} else {
DueDate.clearHelpText()
}


}

 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())
dueDateFld.clearError()
dueDateFld.clearHelpText()
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 10.90.85.64 /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 10.90.85.64 /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>
java.lang.NullPointerException
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
at com.atlassian.jira.issue.Issue$getCustomFieldValue$14.call(Unknown Source)
at Script1.run(Script1.groovy:80)

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 showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

247 views 1 2
Join discussion

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