Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Generating template email out of scriptrunner post functions

Edited

I wanted a bit more control over the custom email messages that were trigger by my workflow so I used the following mechanism.  In the "Condition and Configuration" section I have something like the following:

// Reads a template file from disk and sets ${mailSubject} and ${mailBody}

import groovy.text.GStringTemplateEngine

// insert additional bindings so template can use (for example) ${issue.reporter.displayName}
def binding = [issue: issue]

String TEMPLATE_PREFIX_PATH = "/some/path/to/email/template/foo"
String SUBJECT_TEMPLATE_PATH = TEMPLATE_PREFIX_PATH + "_subject.vm"
String BODY_TEMPLATE_PATH = TEMPLATE_PREFIX_PATH + "_body.vm"

def engine = new GStringTemplateEngine()
def template = engine.createTemplate(new File(SUBJECT_TEMPLATE_PATH)).make(binding)
config.mailSubject = template.toString()

template = engine.createTemplate(new File(BODY_TEMPLATE_PATH)).make(binding)
config.mailBody = template.toString()

// your condition test goes here
return true

Then in the "Email template" section I have ${mailBody} and the "Subject template" has ${mailSubject}.

Hopes this helps others.  Any better ways to do it?

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jul 19, 2023

I do a similar thing, but where it makes sense, I also store the template in an Insight object so that it's easy to update.

A couple of things I might suggest

1) Add a try/catch block because if you have a ${variable} in your template that you don't supply in the binding, the script will just crash. You might want to have some fall-back options.

2) Don't use .vm extension. I think that implies a "velocity template" which is a different kind of template with a different language and syntax.

I use the SimpleTemplateEngine instead of the GStringTemplate engine. I think for your specific use case, it would also work. According to the documentation the GStringTemplateEngine "stores the template as writeable closures (useful for streaming scenarios)", I'm not sure what that means in practical terms.

TAGS
AUG Leaders

Atlassian Community Events