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

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)

 

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

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)

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
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...

181 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