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

Crs co November 16, 2017

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

1 vote
Crs co November 25, 2017

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
}
1 vote
Joshua Yamdogo @ Adaptavist
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 20, 2017

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

Crs co November 21, 2017

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

 

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 21, 2017

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.

0 votes
Crs co November 22, 2017

please guide me to solve it

0 votes
Crs co November 21, 2017

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

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 21, 2017

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"
Crs co November 21, 2017

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events