Scriptruuner groovy date null

Hllo

 

Getting null exception 

java.lang.NullPointerException: Cannot invoke method getTime() on null object at java_sql_Timestamp$getTime.call(Unknown Source) at Script37.run(Script37.groovy:28)

 

How com after checking resolution for null date can be null

How can I check for Date null condition

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.builder.JqlClauseBuilder
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.Project

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def projectManager = ComponentAccessor.getProjectManager()
import org.apache.log4j.Category
def Category log = Category.getInstance("com.samsung.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
Project project = ComponentAccessor.getProjectManager().getProjectObjByKey("JPM");
//CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("");

IssueManager issueManager = ComponentAccessor.getIssueManager();

log.debug ("Key | Created | Resolved | Elapsed")

for(Issue issue: issueManager.getIssueObjects( issueManager.getIssueIdsForProject(project.getId()))){

if (issue.issueType.name == "Story")
if ( issue.getResolution() != null ) {
log.debug(issue.getKey()+"|"+ new Date( issue.getCreated().getTime())+ "|"+
new Date(issue.resolutionDate .getTime())
+"|"+
(issue.resolutionDate.getTime() - issue.getCreated().getTime()) / 1000 / 3600 / 24 +" | "+
issue.getComponents()*.name)
}
}

 

 

Also how do I get value from a custom field called "mycustom"

 

Please let me know

 

Thanks

 

2 answers

1 accepted

1 vote
Henning Tietgens Community Champion Aug 04, 2017

In case you want to restrict your issues, you should use a search like this.

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.web.bean.PagerFilter

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)

def jqlSearch = "project = JPM and created > '2017-05-01'"
List<Issue> issues = null
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def result = ""

SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues
result += "Found ${issues.size()} issues."
} else {
result += "Invalid JQL: " + jqlSearch
}
result

Henning

Henning Tietgens Community Champion Aug 04, 2017

For searching for issues created in the last two weeks you could use

project = JPM and created > startOfWeek(-2w)

for the complete last two weeks or

project = JPM and created > -2w

 for exactly the last two weeks.

Thanks for he help Henning.

I have already accepted answer, just a followup question...

Like we use eclipse for java programming which helps with syntax, api suggestions etc what editor to use to develop grrovy scripting.

Iam looking for some thing which can suggest available methods on object, code hlp etc just like clipse.

 

Is there any editor which can suggest Jira grrovy api while creating a script? What do you use..?

 

I was looking at https://community.atlassian.com/t5/Answers-Developer-Questions/Where-to-start-looking-when-learning-Groovy-for-JIRA/qaq-p/577129

but it looks dated.

Henning Tietgens Community Champion Aug 06, 2017

Just look in the documentation, there is a chapter about setting up a development environment: https://scriptrunner.adaptavist.com/latest/jira/DevEnvironment.html

Henning

0 vote
Henning Tietgens Community Champion Aug 04, 2017

This shouldn't be the case, maybe corrupt data? Try this in the script console:

import com.atlassian.jira.component.ComponentAccessor

def project = ComponentAccessor.projectManager.getProjectObjByKey("JPM")
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def cf = customFieldManager.getCustomFieldObjectsByName('mycustom').first()

def result = "Key | Created | Resolved | Elapsed | Components | myname<br>"

issueManager.getIssueObjects( issueManager.getIssueIdsForProject(project.getId())).each{ issue ->
if (issue.issueType.name == "Story" && issue.resolution) {
def duration
if (issue.resolutionDate) {
duration = (issue.resolutionDate.getTime() - issue.getCreated().getTime()) / 1000 / 3600 / 24
}
result += "$issue.key|" +
"${issue.getCreated()?.format("YYYY-MM-dd HH:mm:ss")}|"+
"${issue.resolutionDate?.format("YYYY-MM-dd HH:mm:ss")}|" +
"$duration|" +
"${issue.getComponents()*.name?.join(',')}|" +
"${issue.getCustomFieldValue(cf)}"
"<br>"
}
}
result

This also shows you how to get the value of a custom field. In this case I suspect a text field. If it's a different field type, you have to do a little more, like issue.getCustomFieldValue(cf)?.value if it's a select field.

Henning

Thanks Henning

it works great.

If I need to look at issues that are created 

- Only after a certain date 

- Only last 2 weeks 

How do I compare dates?

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

113 views 0 3
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