Groovy script - help

Sanu Soman
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 4, 2013

Hi All,

I'm trying to write a groovy script to auto populate number of issues on a JIRA issue filter (just count) to a custom field as part of workflow transition.

Below the sample script,

String jqlSearch = "filter= \"" + demo + "\"";
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService .class);
ParseResult parseResult = searchService.parseQuery(authenticationContext.get LoggedInUser(), jqlSearch);
int totalIssues = 0;
if (parseResult.isValid()) {
try {
SearchResults results = searchService.search(authenticationContext.getLogg edInUser(), parseResult.getQuery(), PagerFilter
.getUnlimitedFilter());
final List issues = results.getIssues();
totalIssues = issues.size();
//add code here to update custom field
MeD = cfm.getCustomFieldObjects(issue).find {it.name == 'IssueNumber'};
setCustomFieldValue(MeD, totalIssues);
} catch (SearchException e) {
e.printStackTrace();
}
}
I'm not getting excepted result with this script and I'm very much new to groovy world, so can't figure out the issue. Can anybody from here help me to sort it out.. please?

Many thanks in advance.

5 answers

1 accepted

1 vote
Answer accepted
RambanamP
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 4, 2013

try some thing like this

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.bc.issue.search.SearchService.ParseResult;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.query.Query;
import com.atlassian.jira.jql.builder.JqlClauseBuilder;
import com.atlassian.jira.jql.builder.JqlQueryBuilder;
import com.atlassian.jira.issue.search.SearchProvider;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.customfields.option.Option;
   
JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService .class);
JqlClauseBuilder builder = JqlQueryBuilder.newClauseBuilder();
CustomField cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("custom field name1"); 
Option cf1Value=null;
if(cf1 !=null)
 cf1Value=(Option)issue.getCustomFieldValue(cf1); 
 
Option cf2Value=null
CustomField cf2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("custom field name2"); 
if(cf2 !=null)
 cf2Value=(Option)issue.getCustomFieldValue(cf2);
 int totalIssues=0;
 if(cf1Value != null && cf2Value != null){
Query query=builder.project().eq("demo").and().customField(cf1.getIdAsLong()).Eq(cf1Value.getValue()).and().customField(cf2.getIdAsLong()).Eq(cf2Value.getValue()).buildQuery();
SearchProvider searchProvider = ComponentAccessor.getComponentOfType(SearchProvider.class);
SearchResults searchResults = searchProvider.search(query, authenticationContext.getLoggedInUser(), PagerFilter.getUnlimitedFilter());
List<Issue> searchIssues = searchResults.getIssues();
 totalIssues = searchIssues.size();
 }
IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager cfm=ComponentAccessor.getCustomFieldManager(); 
def MeD =cfm.getCustomFieldObjectByName("IssueNumber");
issue.setCustomFieldValue(MeD, Integer.toString(totalIssues));
issueManager.updateIssue(authenticationContext.getLoggedInUser(),issue,EventDispatchOption.DO_NOT_DISPATCH,true);

RambanamP
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 4, 2013

use following import

import com.atlassian.jira.issue.search.SearchProvider;

Sanu Soman
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 4, 2013

Getting error,

2013-11-05 13:04:19,338 http-bio-8443-exec-24 ERROR admin 784x18837x1 dzusjs 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script36.groovy: 19: unable to resolve class SearchProvider
@ line 19, column 16.
SearchProvider searchProvider = ComponentAccessor.getComponentOfType(SearchProvider.class);


Sanu Soman
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 4, 2013

error,

2013-11-05 13:59:51,183 http-bio-8443-exec-8 ERROR admin 839x18859x1 dzusjs 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.jql.builder.DefaultJqlClauseBuilder.customField() is applicable for argument types: (java.lang.String) values: [Application]
Possible solutions: customField(java.lang.Long)
2013-11-05 13:59:51,184 http-bio-8443-exec-8 ERROR admin 839x18859x1 dzusjs 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.jql.builder.DefaultJqlClauseBuilder.customField() is applicable for argument types: (java.lang.String) values: [Application]
Possible solutions: customField(java.lang.Long)

RambanamP
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 4, 2013
Sanu Soman
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 5, 2013

Or is it possible to fetch the custom field value from same issue (from create screen) to the groovy script jql query.

example,

jql query - project = demo and customfeild1 = "customfield1 value from same issue" and customfield2 = "customfield2 value from same issue"

Hope its clear.

Sanu Soman
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 5, 2013

Error,

2013-11-06 06:50:06,706 http-bio-8443-exec-16 ERROR admin 410x19227x1 dzusjs 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.jql.builder.DefaultConditionBuilder.Eq() is applicable for argument types: (java.lang.String) values: [CRM WORKBENCH]
Possible solutions: eq(java.lang.String), eq(), eq(com.atlassian.query.operand.Operand), eq(java.lang.Long), eq(java.util.Date), gt(java.lang.String)
2013-11-06 06:50:06,707 http-bio-8443-exec-16 ERROR admin 410x19227x1 dzusjs 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.jql.builder.DefaultConditionBuilder.Eq() is applicable for argument types: (java.lang.String) values: [CRM WORKBENCH]

Here, "CRM WORKBENCH" is one of the values in customfield1. Please suggest.

RambanamP
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 5, 2013

can you do the query on issue navigator and share that query here so i will give you the equevalent query in jira api

Sanu Soman
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 5, 2013

I'm talking about, is it possible to have a dyanmic jql query in groovy script. Means, whenever I"m creating an issue, selecting two mandatory custom field (select list field) values like, customfeild1 = "first value" & customfield2 = "1st value".

So, is it possible to add these custom field values over there in groovy script jql search? ie here, jql becomes

project= "demo" and customfield1 = "first value" and customfield2 = "1st value".

If issue creating with some other values in custom field like, customfield1 = "test value" and customfield2 = "first value", so then jql becomes,

project = "demo" and customfield1 = "test value" and customfield2 = "first value"

Is this possible?

RambanamP
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 5, 2013

yes we can do this, i have updated the answer, try with that

Sanu Soman
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 5, 2013

Getting error,

2013-11-06 08:03:37,576 http-bio-8443-exec-15 ERROR admin 483x19435x1 1fbhjrs 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
2013-11-06 08:03:37,576 http-bio-8443-exec-15 ERROR admin 483x19435x1 1fbhjrs 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(Unknown Source)

Do you need full error log?

JamieA
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 5, 2013

Eq() should be lower-case.

Log the value of the two custom fields and their values and check they are not null.

Sanu Soman
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 5, 2013

I just made "eq()" and here cuustom fields are mandaory field but still getting error,

2013-11-06 08:17:58,850 http-bio-8443-exec-1 ERROR admin 497x19533x1 1g32ekn 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
2013-11-06 08:17:58,850 http-bio-8443-exec-1 ERROR admin 497x19533x1 1g32ekn 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at com.onresolve.jira.groovy.GroovyRunner.runFile(GroovyRunner.java:102)
at com.onresolve.jira.groovy.GroovyRunner.run(GroovyRunner.java:62)
at com.onresolve.jira.groovy.GroovyFunctionPlugin.execute(GroovyFunctionPlugin.java:35)
at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1050)
at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1446)
at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:615)
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:846)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:490)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:428)
at com.atlassian.jira.bc.issue.DefaultIssueService.create(DefaultIssueService.java:184)
at com.atlassian.jira.bc.issue.DefaultIssueService.create(DefaultIssueService.java:149) <+2>
at java.lang.reflect.Method.invoke(Unknown Source)

RambanamP
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 5, 2013

replace query with following code

Query query=builder.project().eq("demo").and().customField(cf1.getIdAsLong()).Eq(cfValues['custom field name1'].value).and().customField(cf2.getIdAsLong()).Eq(cfValues['custom field name2'].value).buildQuery();

is select list fields are not mandatory?

and make sure that you have changed the custom field names

Sanu Soman
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 5, 2013

After using new query, getting error like,

2013-11-06 08:26:30,434 http-bio-8443-exec-10 ERROR admin 506x19553x1 1g32ekn 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: cfValues for class: Script44
2013-11-06 08:26:30,434 http-bio-8443-exec-10 ERROR admin 506x19553x1 1g32ekn 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: cfValues for class: Script44
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(Unknown Source)

RambanamP
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 5, 2013

i have updated my answer again, all my code is based on java syntax and i don't have knowldge on groovy syntax

i think some one who have good knowldge on groovy(@Jamie) can resolve this fast

JamieA
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 5, 2013

It's not a syntax issue, but I will happily write it for him if he pays me.

cfValues is not available in all contexts, he should use customfieldmanager etc to get the cf value.

Sanu Soman
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 5, 2013

Any suggestions on below error,

2013-11-06 09:19:06,710 http-bio-8443-exec-13 ERROR admin 559x19803x1 1a6f5bb 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: java.lang.IllegalArgumentException: Source GenericValue can not be null.
2013-11-06 09:19:06,711 http-bio-8443-exec-13 ERROR admin 559x19803x1 1a6f5bb 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.IllegalArgumentException: Source GenericValue can not be null.
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(Unknown Source)

Sanu Soman
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 5, 2013

I just moved the workflow script runner post function to issue's first status and getting below error,

2013-11-06 10:02:55,995 http-bio-8443-exec-1 ERROR admin 602x20066x1 j43i17 147.60.54.185 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
2013-11-06 10:02:55,995 http-bio-8443-exec-1 ERROR admin 602x20066x1 j43i17 147.60.54.185 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(Unknown Source)
at com.onresolve.jira.groovy.GroovyRunner.runFile(GroovyRunner.java:102)

Previusoly tried the same from create issue transistion and getting specific "Generic value can not be null" error.

Please suggest.

RambanamP
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 5, 2013

which line it throwing null pointer exception?

JamieA
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 5, 2013

Make it the last post-function in the list.

Sanu Soman
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 5, 2013

Caused by: javax.script.ScriptException: java.lang.NullPointerException
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)
... 192 more
Caused by: java.lang.NullPointerException
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:948)
at com.atlassian.jira.issue.MutableIssue$setCustomFieldValue.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
at com.atlassian.jira.issue.MutableIssue$setCustomFieldValue.call(Unknown Source)
at Script48.run(Script48.groovy:42)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)
... 193 more

Sanu Soman
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 5, 2013

I made to last in the list but still getting same error.

which line it throwing null pointer exception? -> How I can find this?

JamieA
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 5, 2013

at Script48.run(Script48.groovy:42)

1 vote
Sanu Soman
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 7, 2013

Any help Rambanam??

RambanamP
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 7, 2013

Sorry Sanu!! i tried my best to help you!

i can't help more than this because i never worked on Groovy and script runner plugin :(

JamieA
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 7, 2013

Sanu, you need to put a bit more effort into this. To log the output use

log.warn(variable)

You will find the output in atlassian-jira.log.

Sanu Soman
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 11, 2013

Hi Rambanam & Jamie,

Thanks a lot for your valuable help. Finally it got worked :) :)

Could you please suggest additionaly how I can add "issue created within this week" parameter to jql query?

Current one is - Query query=builder.project().eq("DEMO").and().customField(cf1.getIdAsLong()).eq(cf1Value.getValue()).and().customField(cf2.getIdAsLong()).eq(cf2Value.getValue()).buildQuery();

Sanu Soman
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 11, 2013

createdAfter("-7d") is working but need something like created >= -1w. Please suggest.

Sanu Soman
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 11, 2013

createdAfter("-1w") is working, but want to find specifically from start of this week like, created >= startOfWeek(). Please suggest.

1 vote
Sanu Soman
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 5, 2013

Hi Rambanam,

Any suggestions?

RambanamP
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 5, 2013

what is the code of at Script48.run(Script48.groovy:42)?

what is the jira version?

Sanu Soman
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 5, 2013

JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService .class);
JqlClauseBuilder builder = JqlQueryBuilder.newClauseBuilder();
CustomField cf1 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Application");
Option cf1Value=null;
if(cf1 !=null)
cf1Value=(Option)issue.getCustomFieldValue(cf1);

Option cf2Value=null
CustomField cf2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Issue category");
if(cf2 !=null)
cf2Value=(Option)issue.getCustomFieldValue(cf2);
int totalIssues=0;
if(cf1Value != null && cf2Value != null){
Query query=builder.project().eq("FX QA Support").and().customField(cf1.getIdAsLong()).eq(cf1Value.getValue()).and().customField(cf2.getIdAsLong()).eq(cf2Value.getValue()).buildQuery();
SearchProvider searchProvider = ComponentAccessor.getComponentOfType(SearchProvider.class);
SearchResults searchResults = searchProvider.search(query, authenticationContext.getLoggedInUser(), PagerFilter.getUnlimitedFilter());
List<Issue> searchIssues = searchResults.getIssues();
totalIssues = searchIssues.size();
}
IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager cfm=ComponentAccessor.getCustomFieldManager();
def MeD =cfm.getCustomFieldObjectByName("Bronzeissue");
issue.setCustomFieldValue(MeD, Integer.toString(totalIssues));
issueManager.updateIssue(authenticationContext.getLoggedInUser(),issue,EventDispatchOption.DO_NOT_DISPATCH,true);

Sanu Soman
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 5, 2013

are you looking for 42nd line? then,

issue.setCustomFieldValue(MeD, Integer.toString(totalIssues));

Jira - 6.0.5

Can you please guide what does this "Script48.run(Script48.groovy:42)" mean? which line I need to check for this??

RambanamP
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 5, 2013

can you replace last line with following code

issueManager.updateIssue(authenticationContext.getUser().getDirectoryUser(),issue,EventDispatchOption.DO_NOT_DISPATCH,true);

and also give prints and check

JamieA
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 5, 2013

> Can you please guide what does this "Script48.run(Script48.groovy:42)" mean

It means see line 42 of your script.

Sanu Soman
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 6, 2013

Hi Rambanam,

Still getting same "NullPointerException" error.

2013-11-07 06:43:22,687 http-bio-8443-exec-10 ERROR admin 403x20583x1 1ay98xf 147.60.54.185 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)

It showing as lline 42 in script having the issue,

at Script52.run(Script52.groovy:42)

ie - issue.setCustomFieldValue(MeD, Integer.toString(totalIssues));

RambanamP
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 6, 2013

print all the values like current issue, custom field names and it's value

Sanu Soman
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 6, 2013

Sorry.. how I can do that?

Sanu Soman
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 6, 2013

is it - document.writeln("Last Modified: " + cf1Value)??

so it will be,

if(cf1 !=null)
cf1Value=(Option)issue.getCustomFieldValue(cf1);
document.writeln("Last Modified: " + cf1Value)

Also do I need to add any additonal import lines to this script?

1 vote
RambanamP
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 4, 2013

try witht his script

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.bc.issue.search.SearchService.ParseResult;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.issue.CustomFieldManager;



String jqlSearch = "filter= \"" + filterName + "\"";
JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
SearchService searchService = ComponentAccessor.getComponentOfType(SearchService .class);
ParseResult parseResult = searchService.parseQuery(authenticationContext.getLoggedInUser(), jqlSearch);
int totalIssues = 0;
if (parseResult.isValid()) {
SearchResults results = searchService.search(authenticationContext.getLoggedInUser(), parseResult.getQuery(), PagerFilter
.getUnlimitedFilter());
final List issues = results.getIssues();
totalIssues = issues.size();
//add code here to update custom field
IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager cfm=ComponentAccessor.getCustomFieldManager(); 
def MeD =cfm.getCustomFieldObjectByName("IssueNumber");
issue.setCustomFieldValue(MeD, totalIssues);
issueManager.updateIssue(authenticationContext.getLoggedInUser(),issue,EventDispatchOption.DO_NOT_DISPATCH,true);

}

don't forget to add error messages here if you got any

Sanu Soman
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 4, 2013

Getting error like,

2013-11-05 08:01:03,347 http-bio-8443-exec-20 ERROR admin 481x17117x1 e5wfg3 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: demo for class: Script22
2013-11-05 08:01:03,348 http-bio-8443-exec-20 ERROR admin 481x17117x1 e5wfg3 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: demo for class: Script22
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(Unknown Source)

RambanamP
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 4, 2013

the filter name should be something like this

String jqlSearch = "filter= \"demo\"";

Sanu Soman
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 4, 2013

Now getting,

2013-11-05 08:20:10,996 http-bio-8443-exec-22 ERROR admin 500x17670x1 fazx5 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: PagerFilter for class: Script23
2013-11-05 08:20:10,996 http-bio-8443-exec-22 ERROR admin 500x17670x1 fazx5 147.60.54.185 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: PagerFilter for class: Script23
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(Unknown Source)

RambanamP
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 4, 2013

add the following import

import com.atlassian.jira.web.bean.PagerFilter;

Sanu Soman
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 4, 2013

Sorry.. now getting,

2013-11-05 08:43:16,565 http-bio-8443-exec-22 ERROR admin 523x17789x1 4jb22a 147.60.54.185 /secure/CreateIssueDetails.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
2013-11-05 08:43:16,565 http-bio-8443-exec-22 ERROR admin 523x17789x1 4jb22a 147.60.54.185 /secure/CreateIssueDetails.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
javax.script.ScriptException: javax.script.ScriptException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(Unknown Source)

My custom field (IssuNumber) is a free text field, is that causing the issue here?

RambanamP
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 4, 2013

yes, you are correct!

you need to pass string as a paramenter to following method

issue.setCustomFieldValue(MeD, totalIssues);

Sanu Soman
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 4, 2013

If you don't mind, how it will be?

RambanamP
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 4, 2013

you can try with

Integer.toString(totalIssues)
 or 
String.valueOf(totalIssues)

finally it should be like this

issue.setCustomFieldValue(MeD, Integer.toString(totalIssues));

Sanu Soman
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 4, 2013

Many thanks Rambanam for the valuable help.

Cheers!!

RambanamP
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 4, 2013

Glad to hear it worked!!

i hope you can close following issue by accepting my as answer as correct :)

https://answers.atlassian.com/questions/223702/custom-field-to-auto-dispaly-number-of-issues-in-an-issue-filter

Sanu Soman
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 4, 2013

Hi Rambanam,

Is it possible to add custom jql search instead of saved issue filter in above groovy script itself?, If so, what are the changes needs to be performed?

jql example - project = demo and customfield = "ABC" and customfeild2 = "123"

RambanamP
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 4, 2013

yes we can we can add custom jql search, check my another answer!!

0 votes
JamieA
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 4, 2013

Can you remove the try/catch... that is sending the exception to stdout, perhaps you are looking at the wrong log file.

Where is setCustomFieldValue(..) defined?

Where are you specifying on which issue to update the CF?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events