Custom field insertion in e-mail subject (GStringTemplate)

Nabil Sayegh
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.
January 14, 2013

I'm trying to send a mail via a transition's post-function.

The subject and body fields say, you can enter GStringTemplates, but I can't find any decent documentation about that.

How do I simply insert e.g. custom field foo (id 12345) to the subject and/or body?

2 answers

1 accepted

1 vote
Answer accepted
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.
January 14, 2013

http://groovy.codehaus.org/api/groovy/text/GStringTemplateEngine.html

It's a pain doing it in the subject. You need something like this:

Issue ID: ${issue.id}, Custom Field Value: <% out << issue.getCustomFieldValue(componentManager.getCustomFieldManager().getCustomFieldObjectByName("My custom field")) %>

Nabil Sayegh
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.
January 14, 2013

Thank you.

I'll try that, but before that I have to configure a test smtp.

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.
January 14, 2013

You can use the "preview" function to see what the subject will look like. This is highly recommended before firing off emails.

Nabil Sayegh
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.
January 14, 2013

Interesting. Where can I see the preview?

I can configure the field "Preview Issue Key" when editing the transition, but I don't see no button "Preview".

Nabil Sayegh
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.
January 14, 2013

I tried with this as subject:

"Es wurde ein neues Artefakt $issue zum Deployment gemeldet. cfValues['customfield_12714']".

But it doesn't resolve the custom field and results in this subject:

"[TEST] Es wurde ein neues Artefakt LUUS-1126 zum Deployment gemeldet. cfValues['customfield_12714']"

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.
January 14, 2013

Answer updated...

Nabil Sayegh
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.
January 15, 2013

Thanks for the update. But I fear, it's still not working:

<% out << issue.getCustomFieldValue(componentManager.getCustomFieldManager().getCustomFieldObjectByName("customfield_12714")) %>

2013-01-16 08:33:47,003 http-8081-17 ERROR nabil.sayegh@example.com 513x22538x1 1o7xo1j 10.109.54.241,127.0.0.1 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
java.lang.NullPointerException
	at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:938)
	at com.atlassian.jira.issue.Issue$getCustomFieldValue.call(Unknown Source)
	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
	at groovy.tmp.templates.GStringTemplateScript6$_getTemplate_closure1.doCall(GStringTemplateScript6.groovy:2)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
	at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
	at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:272)
	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:884)
	at groovy.lang.Closure.call(Closure.java:412)
	at groovy.lang.Closure$WritableClosure.writeTo(Closure.java:828)
	at groovy.lang.Closure$WritableClosure.toString(Closure.java:954)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
...

Nabil Sayegh
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.
January 15, 2013

I also tried with :

<% out << issue.getCustomFieldValue(componentManager.getCustomFieldManager().getCustomFieldObjectById(12714)) %>

and

<% out << issue.getCustomFieldValue(componentManager.getCustomFieldManager().getCustomFieldObjectById(12714L)) %>

But am getting these errors:

2013-01-16 08:51:51,617 http-8081-22 ERROR nabil.sayegh@example.com 531x22643x1 j2tplz 10.109.54.241,127.0.0.1 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.managers.DefaultCustomFieldManager.getCustomFieldObjectById() is applicable for argument types: (java.lang.Long) values: [12714]
Possible solutions: getCustomFieldObject(java.lang.Long), getCustomFieldObjects(), getCustomFieldObject(java.lang.String), getCustomFieldObjectByName(java.lang.String), getCustomFieldObjects(com.atlassian.jira.issue.Issue), getCustomFieldObjects(com.atlassian.jira.issue.search.SearchContext)
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.
January 15, 2013

You need the name of the custom field, not the ID. The name is the name that you see when editing an issue in the UI. If you want to use the ID, change getCustomFieldObjectByName to another method.

Nabil Sayegh
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.
January 15, 2013

Works!

Thank you.

BTW: I tried getCustomFieldObjectById() but it didn't work because it expects a java Long. Do you know how to specify that in Groovy? I tried without quotes and with and without L.

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.
January 15, 2013

Use either `1 as Long` or `1L`, or `new Long(1)`, no quotes in either case.

0 votes
guis March 20, 2013

Hello,

I use the plugin script runner and use the script post-function. I use the script "Sending a custom email on a transition'. How insert the value of the custom field present in the issue in the template of the mail? How recove the value by id?

Guis

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.
March 21, 2013

Isn't this question the same as the one you are asking? Why do you need the custom field ID? If you need it you can get it from the view custom fields page, or from the db.

guis March 21, 2013

Thank you. How I can to recove the name of status by groovy? I tried issue.status?.name but it doesn't work.

Guis

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.
March 21, 2013

can you explain doesn't work. That should work but issue.statusObject.name is probably better.

guis March 21, 2013

Thank you for the answer. I forget "$".

Rocky July 23, 2014

HI jaime,

Currently we are using jira 6.0.5

we need to display the custom field value in the subject of the email whenever the respective field is updated

the field is a version picker

currently we are using the text mode for email notification.i have edited the issueupdated.vm.i have tried the below template

# if the custom field value is empty print the issuekey.issuetype,and summary else print issuekey,updated custom field value and issue summary

if($issue.getCustomFieldValue("customfield_1001") = "")

($issue.key) ($issue.issueTypeObject.name) $issue.summary

#else
($issue.key) $issue.getCustomFieldValue("customfield_1001") ($issue.issueTypeObject.name) $issue.summary
#end

im not getting the value when the custom field is updated.

please help me with valid template

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.
July 24, 2014

that's a velocity template, not related to my plugin

Suggest an answer

Log in or Sign up to answer