Groovy Script - Comment with cFields

Hello everybody,

we use a groovy script which append a comment to the issue where i executes the transition.
At moment it works but i want that the string in a textfield in the same issue should be included in the comment as well.

here is the script:

import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.*;

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

//Zusammenbau vom String für den Kommentar
sourceFieldVal = ""+issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10603")).displayName

sourceFieldVal = "Hallo *" + sourceFieldVal + "* \
\n\ndeine Hardware steht zur Abholung bereit.\
\n\nGrüsse\
\nTechnical Services"

CommentManager commentMgr = ComponentAccessor.getCommentManager()
commentMgr = (CommentManager) ComponentAccessor.getComponentOfType(CommentManager.class)

//Der wirkliche Kommentar
commentMgr.create(issue, currentUser, sourceFieldVal, true)

the custom-field is called hostname.

can somebody help?

Hi Antonio,
can you specify how you are running this Groovy script? JMWE? ScriptRunner?
David

Hello David,

i use ScriptRunner.

Toni

It looks like you already have the code for this - what doesn’t work?

The field “Hostname” is a single-line-text-field.

Here is the updated script:

import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.*;

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

//Zusammenbau vom String für den Kommentar
sourceFieldVal = ""+issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10603")).displayName
customField_name = ""+issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(Hostname))
wert = issue.getCustomFieldValue(customField_name)

sourceFieldVal = "Hallo *" + sourceFieldVal + "* \
\n\ndeine Hardware steht zur Abholung bereit.\
\n\nGrsse *" + wert + "*\
\nTechnical Services"

CommentManager commentMgr = ComponentAccessor.getCommentManager()
commentMgr = (CommentManager) ComponentAccessor.getComponentOfType(CommentManager.class)

//Der wirkliche Kommentar
commentMgr.create(issue, currentUser, sourceFieldVal, wert, true)

And here is the error:
2017-04-03 14:41:04,494 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-04-03 14:41:04,495 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ITSD-127, actionId: 101, file: /data/jira/home/groovy/comment.groovy
groovy.lang.MissingPropertyException: No such property: Hostname for class: comment
at comment.run(comment.groovy:12)

Looks like Hostname should be quoted?

you mean in the comment itself? no it does not have to.

Should be

customField_name = ""+issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Hostname"))

2017-04-07 13:25:46,861 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-04-07 13:25:46,861 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ITSD-128, actionId: 101, file: /data/jira/home/groovy/comment.groovy
java.lang.NullPointerException: Cannot get property ‘displayName’ on null object
at comment.run(comment.groovy:11)

customfield_10603 is null then… you need to check that or use ?.displayName

hello jamie,

i want to include the value in that cf in the comment.

import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.*;

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

//Zusammenbau vom String für den Kommentar
sourceFieldVal = ""+issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10603")).displayName
customField_name = ""+issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Hostname")).displayName
wert = issue.getCustomFieldValue(customField_name)

sourceFieldVal = "Hallo *" + sourceFieldVal + "* \
\n\ndeine Hardware steht zur Abholung bereit.\
\n\nGrsse *" + wert + "*\
\nTechnical Services"

CommentManager commentMgr = ComponentAccessor.getCommentManager()
commentMgr = (CommentManager) ComponentAccessor.getComponentOfType(CommentManager.class)

//Der wirkliche Kommentar
commentMgr.create(issue, currentUser, sourceFieldVal, wert, true)

2017-04-07 13:27:00,155 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-04-07 13:27:00,155 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: ITSD-128, actionId: 101, file: /data/jira/home/groovy/comment.groovy
java.lang.NullPointerException: Cannot get property ‘displayName’ on null object
at comment.run(comment.groovy:11)

we are talking about the cf “Hostname” with the cf_id customfield_10500 and not about the cf_id "customfield_10603

hello jamie,

should i raise an issue on the support-site? Ultimately, we have a valid license and want this to work.

best regards
toni

but clearly, given the exception you are getting, displayName is null, which refers to the 603 CF.

The Hostname part would probably work fine once you fix that issue.

We want it to work too, but our support EULA does not cover debugging user code.

Hi Antonio,
Try adding swapping this code for the respective lines in yours:

customField_name = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Hostname")
wert = issue.getCustomFieldValue(customField_name)

It looks like you were trying to pass a string to the getCustomFieldValue() where you need to be passing a CustomField object.

Regards,
Johnson

2 Likes

herllo jamie,

i know and a apologize for being a little frustrating. i work since 2010 with the atlassian-tools and the first plugin which i installed was script runner :slight_smile:

for me script runner is the best plugin regarding the customization for workflows. it is very powerful.

have a nice day.
toni

hello johnson,

it works. thx a lot and to everybody else which help me out.

best regards
toni