Error i am getting in Script Runner

Hi All

I have Script runner installed version 4.1.2 on production JIRA  version 6.4.12.

I have two script which is giving error while doing JIRA re-indexing for some issues not all the issue we having in jira.

For getting members:

 

import com.atlassian.jira.component.ComponentAccessor
def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Group to maintain")
def theusers = groupManager.getUsersInGroup(issue.getCustomFieldValue(cf))
def thetext = ""
theusers.each() {
thetext += it.getDisplayName() + " (" + it.getName() + ")<br>"
}
return thetext

For getting age

import com.atlassian.jira.component.ComponentAccessor
def groupManager = ComponentAccessor.getGroupManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Date Opportunity Identified")
def dateOppIdef = issue.getCustomFieldValue(cf)
def ageInDays="";
if(dateOppIdef!=""){
ageInDays=new Date() - dateOppIdef;
}
return ageInDays.toString();


 

The error is as such

 

2016-12-04 09:32:38,914 IssueIndexer:thread-19 ERROR asin17 563x205x1 1ily4nh 14.140.116.135 /secure/admin/IndexReIndex.jspa [onresolve.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: SSGM-2582, field: Members
org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method getUsersInGroup() to invoke from this list:
  public abstract java.util.Collection com.atlassian.jira.security.groups.GroupManager#getUsersInGroup(com.atlassian.crowd.embedded.api.Group)
  public abstract java.util.Collection com.atlassian.jira.security.groups.GroupManager#getUsersInGroup(java.lang.String)
	at Script1.run(Script1.groovy:5)

 

2016-12-04 09:23:48,058 IssueIndexer:thread-42 ERROR asin17 563x205x1 1ily4nh 14.140.116.135 /secure/admin/IndexReIndex.jspa [onresolve.scriptrunner.customfield.GroovyCustomField] Script field failed on issue: PSGD-410, field: Age (in days)
java.lang.NullPointerException
	at Script3.run(Script3.groovy:8)

 

Also when i am using Script console and try to fix the issue,I am getting below error:

No signature of method: com.atlassian.jira.issue.managers.DefaultIssueManager.getCustomFieldValue() is applicable for argument types: (com.atlassian.jira.issue.fields.CustomFieldImpl) values: [Group to maintain]


1 answer

0 vote
Vasiliy Zverev Community Champion Jan 26, 2017

This code is strange. Here is a refactored vervions:

import com.atlassian.jira.component.ComponentAccessor

StringBuilder thetext = new StringBuilder()
ComponentAccessor.getGroupManager().getUsersInGroup(
        ((ArrayList&lt;Object&gt;) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Group to maintain"))).get(0))
        .each() {
    thetext.append(it.getDisplayName()).append(" (").append(it.getName()).append(")&lt;br&gt;");
}
return thetext.toString()
import com.atlassian.jira.component.ComponentAccessor

import java.sql.Timestamp

Timestamp dateOppIdef = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Date Opportunity Identified"))
int ageInDays = 0;
if(dateOppIdef != null){
     ageInDays = (System.currentTimeMillis() - dateOppIdef.getTime())/(60*60*24*1000) 
}
return String.valueOf(ageInDays);

Hi Vasiliy

Still above code is not working on Script console.I am getting below error

 

Error

No such property: issue for class: Script6

groovy.lang.MissingPropertyException: No such property: issue for class: Script6 at Script6.run(Script6.groovy:5)

 

Vasiliy Zverev Community Champion Jan 30, 2017

When you run script for script condition, validator or postfunction you have predefined variable issue which corresponds to current issue.

If you want to run script via script console you have to define and initialise this variable manually. I usually use this (replace "issueKey" with actial issue key):

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey")

import com.atlassian.jira.component.ComponentAccessor

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("GME-17730")


StringBuilder thetext = new StringBuilder()
ComponentAccessor.getGroupManager().getUsersInGroup(
((ArrayList<Object>) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Group to maintain"))).get(0))
.each() {
thetext.append(it.getDisplayName()).append(" (").append(it.getName()).append(")<br>");
}
return thetext.toString()

 

I run above code in script console  but still getting error.Please check below 

 

Error

startup failed: Script15.groovy: 3: unable to resolve class Issue @ line 3, column 7. Issue issue = ComponentAccessor.getIssueManager().getIssueObject("GME-17730") ^ 1 error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script15.groovy: 3: unable to resolve class Issue @ line 3, column 7. Issue issue = ComponentAccessor.getIssueManager().getIssueObject("GME-17730") ^ 1 error
Vasiliy Zverev Community Champion Jan 31, 2017

Try this one:

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

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("GME-17730")

StringBuilder thetext = new StringBuilder()
ComponentAccessor.getGroupManager().getUsersInGroup(
        ((ArrayList&lt;Object&gt;) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Group to maintain"))).get(0))
        .each() {
    thetext.append(it.getDisplayName()).append(" (").append(it.getName()).append(")&lt;br&gt;");
}
return thetext.toString()

No success!! Getting below error message.

 

Error

Cannot invoke method get() on null object

java.lang.NullPointerException: Cannot invoke method get() on null object at Script18.run(Script18.groovy:8)
Vasiliy Zverev Community Champion Feb 01, 2017

It means that value of custom field for this issue is empty. Here is hew version

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

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("GME-17730")

StringBuilder thetext = new StringBuilder()
try{
    ComponentAccessor.getGroupManager().getUsersInGroup(
            ((ArrayList&lt;Object&gt;) issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Group to maintain"))).get(0))
            .each() {
        thetext.append(it.getDisplayName()).append(" (").append(it.getName()).append(")&lt;br&gt;");
    }
} catch (NullPointerException e){
    return ""
}

return thetext.toString()

Thanks Vasiliy for your support now i will apply your suggested script on JIRA and will update you.

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 Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

70 views 0 5
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