It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Custom field insertion in e-mail subject (GStringTemplate)

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

Thank you.

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

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

Answer updated...

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

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

0 votes
guis Mar 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 Mar 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 Mar 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 Mar 21, 2013

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

guis Mar 21, 2013

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

Rocky Jul 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 Jul 24, 2014

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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

7,113 views 22 35
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you