Can someone please validate the below script to fetch date value from a different field ?

Aisha M
Contributor
May 2, 2018
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
Issue issue = issuedef customFieldManager = ComponentAccessor.getCustomFieldManager()

 

// the dependent custom field

def dateACF = customFieldManager.getCustomFieldObjectByName("Due Date")

// get the value of the Due Date custom field

def dateAValue = issue.getCustomFieldValue(dateACF) as Date

// build the new date

def dateB = Calendar.getInstance()

// copy to the new date the value of the Due Date custom field

dateB.setTime(dateAValue)

return dateB.getTime()

When I run this , I get an error as "java.lang.NullPointerException"

Can someone please help me out. Thanks

1 answer

1 accepted

0 votes
Answer accepted
Alexey Matveev
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.
May 2, 2018

You should add logging to your script and see in the logs, what object gives you a NullPointerException. It could be done like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
Issue issue = issuedef customFieldManager = ComponentAccessor.getCustomFieldManager()

 

// the dependent custom field

def dateACF = customFieldManager.getCustomFieldObjectByName("Due Date")

log.error("dateACF: " + dateACF)

// get the value of the Due Date custom field

def dateAValue = issue.getCustomFieldValue(dateACF) as Date

// build the new date

def dateB = Calendar.getInstance()

// copy to the new date the value of the Due Date custom field

dateB.setTime(dateAValue)

return dateB.getTime()

Aisha M
Contributor
May 2, 2018

@Alexey Matveev Thank you for your reply Alexey. I tried your script above and got the below error,

groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.issuedef() is applicable for argument types: (com.atlassian.jira.issue.managers.CachingCustomFieldManager) values: [com.atlassian.jira.issue.managers.CachingCustomFieldManager@52e105aa]

Alexey Matveev
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.
May 2, 2018

It is strange error. I can not see a reason why it would be caused by the code. Could you try again?

Aisha M
Contributor
May 3, 2018

@Alexey Matveev Tried running it again. Unfortunately getting the same error. 

Alexey Matveev
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.
May 3, 2018

Yes, I got it. Try like this:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()

// the dependent custom field

def dateACF = customFieldManager.getCustomFieldObjectByName("Due Date")

log.error("dateACF: " + dateACF)

// get the value of the Due Date custom field

def dateAValue = issue.getCustomFieldValue(dateACF) as Date

// build the new date

def dateB = Calendar.getInstance()

// copy to the new date the value of the Due Date custom field

dateB.setTime(dateAValue)

return dateB.getTime()

 

Alexey Matveev
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.
May 3, 2018

And the correct script would be like this:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

// get the value of the Due Date custom field

def dateAValue = issue.getDueDate() as Date

// build the new date

def dateB = Calendar.getInstance()

// copy to the new date the value of the Due Date custom field

dateB.setTime(dateAValue)

return dateB.getTime()

The problem is that Due Date is not a custom field and you should get the value of the field by issue.getDueDate()

Aisha M
Contributor
May 3, 2018

@Alexey Matveev Yes, works now !!! Thank you much for helping me out on this :) 

Suggest an answer

Log in or Sign up to answer