Behavior Field generates Cannot invoke method getDueDate() on null object Edited

I added below groovy script to duedate of sau tasks to prevent subtasks accept Duedate value more that their parents, this is work in create issue, but in edit issue, my code does not work and the below error shown in log:

Cannot invoke method getDueDate() on null object

My code is :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
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
FormField DueDate = getFieldById(getFieldChanged())
//def DueDate = getFieldById(getFieldChanged())
//FormField field = getFieldById(getFieldChanged())
FormField parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long

if (!parentIssueId || DueDate.getFormValue()) {
// this is not a subtask, or the field already has data
// return
}

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


def DueSubtask = DueDate.getValue()

Date newDueSubtask = (Date)DueSubtask
Date DueTask = parentIssue.getDueDate()

if(newDueSubtask.compareTo(DueTask)>0) {
DueDate.setHelpText("Duedate can not be after Task Duedate, Insert the a properiate Date")
DueDate.setFormValue("")

} else {
DueDate.clearHelpText()

}

 

4 answers

Hi,

Could you clarify where this script is being used? Your question title says "Scripted Field" but this looks like a script for a behaviour?

@BaseScript FieldBehaviours fieldBehaviours

Josh

yes your are right this is a script for a behaviour.

I map the due date field and select the subtasks of all project for it

 

Have you limited this behaviour to just sub-tasks?  If it's run on parent level issues, it's going to throw this error because the parent of a parent level issue is always going to be null.

It got OK, by help of my friend:

 

The problem is that in the Edit screen field "parentIssueId" does not exist. That is why if it does not exist then you should take the value from the issue. Replace

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

with

FormField parent = getFieldById("parentIssueId")
Long parentIssueId = parent.getFormValue() as Long
if (!parentIssueId) {
    parentIssueId =underlyingIssue?.getParentId() as Long
}

I change the mapping and now it is mapped to all Project and all issue types and the problem still there is.

Sorry, that's the opposite of what I said - this Behaviour either needs to

  • Be used only for sub-task types
  • Have code in it that says "if not sub-task type, try to do nothing"

Yes it check first the issue is sub task or not and it it is then continue. Any way i test the code for both status. Only sub tasks and all issue types. But it only work in create issue and not edit issue

please guide me to solve it

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Thursday in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

355 views 0 6
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you