Groovy post-function (create) add comment with custom field value

Mario Fernandes Meyer March 25, 2013

hi all, we wan't to add a comment on issue creation based on a custom field value. For that we use groovyrunner 1.7.14 plugin on JIRA 4.3.4. I have this script be can't figure out what it's goging wrong:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.comments.CommentManager
import org.apache.log4j.Category
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.core.user.UserUtils
import com.opensymphony.user.User
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.issue.Issue

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
User currentUserObj = UserUtils.getUser(currentUser);

Issue req = issue;
 
compManager = ComponentManager.getInstance()

sourceFieldName = "Customer Name"
CustomFieldManager customFieldManager = compManager.getCustomFieldManager()
CustomField customFieldSrc = customFieldManager.getCustomFieldObjectByName(sourceFieldName)
sourceFieldVal = issue.getCustomFieldValue(customFieldSrc)

compManager.getCommentManager()
commentMgr = (CommentManager) compManager.getComponentInstanceOfType(CommentManager.class)

//create(com.atlassian.jira.issue.Issue, java.lang.String, java.lang.String, boolean)

commentMgr.create(req, currentUserObj, sourceFieldVal, true)
issue.store()

We get this error (atlassian-jira.log):

Caused by: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.comments.DefaultCommentManager.create() is applicable for argument types: (com.atlassian.jira.issue.IssueImpl, com.opensymphony.user.User, java.lang.String, java.lang.Boolean) values: [test Mario, mf, test email, true]

Possible solutions: create(com.atlassian.jira.issue.Issue, java.lang.String, java.lang.String, boolean), create(com.atlassian.jira.issue.Issue, java.lang.String, java.lang.String, java.lang.String, java.lang.Long, boolean), iterator(), create(com.atlassian.jira.issue.Issue, java.lang.String, java.lang.String, java.lang.String, java.lang.Long, java.util.Date, boolean)

at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:54)

at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)

at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)

at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:136)

at Script27.run(Script27.groovy:33)

at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:315)

... 143 more

6 answers

1 accepted

1 vote
Answer accepted
Mario Fernandes Meyer March 25, 2013

With your help, I found the solution! I had to change order of Post functions in the transition (sounds obvious afterwards, my post-script function was called before the issue was created!)

Creates the issue originally. 
— THEN
Script C:/Program Files (x86)/Atlassian/JIRA 4.3.4/atlassian-jira/WEB-INF/classes/groovyscripts/add-SOCIETE-as-comment.groovy will be run. 
Edit | Move Up | Move Down | Delete
— THEN
Re-index an issue to keep indexes in sync with the database. 
— THEN
Fire a Demande créée event that can be processed by the listeners. 
Edit

And here is my script:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.comments.CommentManager
import org.apache.log4j.Category
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.core.user.UserUtils
import com.opensymphony.user.User
import com.opensymphony.workflow.WorkflowContext

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
User currentUserObj = UserUtils.getUser(currentUser);

compManager = ComponentManager.getInstance()

sourceFieldName = "Société"
CustomFieldManager customFieldManager = compManager.getCustomFieldManager()
CustomField customFieldSrc = customFieldManager.getCustomFieldObjectByName(sourceFieldName)
sourceFieldVal = issue.getCustomFieldValue(customFieldSrc)

CommentManager commentMgr = compManager.getCommentManager()
commentMgr = (CommentManager) compManager.getComponentInstanceOfType(CommentManager.class)
commentMgr.create(issue, currentUser, sourceFieldVal, false)

Thanks for all!
Mario

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 19, 2013

Um, share what? Mario has posted his code and the hint that you need to be careful where to place it. What else are you looking for here?

dhaval soni
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 19, 2013

Can you please share ?

I need to execute this during intermediate isssue transition where issue was already created and just need to add comment for which custom field was updated in last transition screen.

can you please share me stuff for that?

thanks

dhaval soni

2 votes
Geoffrey Laparra May 28, 2015

Hello all,

Tear up my hair, but here is one functionnal on JIRA 6.4.3 and script runner plugin.

import com.atlassian.jira.ComponentManagerimport com.atlassian.jira.issue.comments.CommentManager
import org.apache.log4j.Category
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.user.*;
import com.atlassian.jira.component.ComponentAccessor;
compManager = ComponentManager.getInstance()
  
//Gets the conected user on Jira
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
  
//Gets the CustomField value
sourceFieldName = "Reopening reason" // declare here the CFname
CustomFieldManager customFieldManager = compManager.getCustomFieldManager()
CustomField customFieldSrc = customFieldManager.getCustomFieldObjectByName(sourceFieldName)
sourceFieldVal = issue.getCustomFieldValue(customFieldSrc)
sourceFieldVal = "Motif de réouverture du ticket : \n" + sourceFieldVal 
  
CommentManager commentMgr = compManager.getCommentManager()
commentMgr = (CommentManager) compManager.getComponentInstanceOfType(CommentManager.class)
 
 
//Public Comment
//commentMgr.create(issue, currentUser, sourceFieldVal, false)


//OR a RESTRICTED comment to a role(10200 = Internal users role in my case)
//ComponentAccessor.getCommentManager().create(issue, currentUser, sourceFieldVal, null, 10200 as Long, true)

Just get rid of the // on to select the Public or Restricted comment.

Enjoy

0 votes
Uba Akpe August 11, 2013

Please there is still error,despite the help by Henning,try to provide a possible solution because I like this site it will be very useful.

Henning Tietgens
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.
August 11, 2013

You should ask a new question and describe your code and the problem you have...

0 votes
Henning Tietgens
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.
March 25, 2013

Ok, the issue doesn't exists at the time you try to add the comment. You can use

transientVars.comment = sourceFieldVal

to add a comment to the transition.

Henning

Mario Fernandes Meyer March 25, 2013

Thanks for your help Henning. Unfortunatelly, I still get an error because the issue doesn't exist on CreateIssue transition. I replaced

commentMgr = (CommentManager) compManager.getComponentInstanceOfType(CommentManager.class)
commentMgr.create(req, currentUser, sourceFieldVal, true)

by

transientVars.comment = sourceFieldVal

Here is the error in the log:

2013-03-26 15:37:20,484 http-443-19 WARN mf 937x17138x1 pmfwi1 84.253.14.34 /browse/INF-235 [jira.issue.transitions.TransitionLinkFactory] !!! Issue INF-235 has no workflow ID !!!

2013-03-26 15:37:26,766 http-443-19 WARN mf 937x17161x1 pmfwi1 84.253.14.34 /browse/INF-235 [jira.issue.transitions.TransitionLinkFactory] !!! Issue INF-235 has no workflow ID !!!

Henning Tietgens
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.
March 25, 2013

Don't try to store the issue at the end, this is done by the transition itself.

0 votes
Henning Tietgens
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.
March 25, 2013

You should use currentUser instead of currentUserObj for the create() method.

Henning

Mario Fernandes Meyer March 25, 2013

I tried both your solutions but get a java.lang.NullPointerException

2013-03-26 15:21:18,797 http-443-14 WARN mf 921x16227x1 pmfwi1 84.253.14.34 /secure/CreateIssueDetails.jspa [jira.issue.index.DefaultIndexedChangeHistoryFieldManager] The issue null has serious data integrity issues

java.lang.NullPointerException

	at com.atlassian.jira.issue.index.DefaultIndexedChangeHistoryFieldManager$1.buildSyntheticChange(DefaultIndexedChangeHistoryFieldManager.java:41)

	at com.atlassian.jira.issue.index.DefaultChangeHistoryRetriever.filterChangeItems(DefaultChangeHistoryRetriever.java:52)

	at com.atlassian.jira.issue.index.DefaultChangeHistoryRetriever.get(DefaultChangeHistoryRetriever.java:33)

	at com.atlassian.jira.issue.index.DefaultChangeHistoryRetriever.get(DefaultChangeHistoryRetriever.java:17)

	at com.atlassian.jira.issue.index.DefaultIssueIndexer$ChangeHistoryDocumentBuilder.get(DefaultIssueIndexer.java:482)

	at com.atlassian.jira.issue.index.DefaultIssueIndexer$IssueLockDocumentCreationStrategy$2.get(DefaultIssueIndexer.java:514)

	at com.atlassian.jira.issue.index.DefaultIssueIndexer$IssueLockDocumentCreationStrategy$2.get(DefaultIssueIndexer.java:511)

	at com.atlassian.util.concurrent.ManagedLocks$ManagedLockImpl.withLock(ManagedLocks.java:315)

Henning Tietgens
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.
March 25, 2013

Additionally I don't see any sense for the line

compManager.getCommentManager()

on it's own. You could use this to get the commentMgr and skip the next statement, so that it looks like

sourceFieldVal = issue.getCustomFieldValue(customFieldSrc)
 
commentMgr = compManager.getCommentManager()
 
//create(com.atlassian.jira.issue.Issue, java.lang.String, java.lang.String,

Henning

0 votes
DanielP
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.
March 25, 2013

Currently you are passing the User object as a parameter to the method when it only requires a String containing the username.

So change the line

commentMgr.create(req, currentUserObj, sourceFieldVal, true)

to

commentMgr.create(req, currentUserObj.getName(), sourceFieldVal, true)

Suggest an answer

Log in or Sign up to answer