cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Running a rule from an EmailTemplate

Running a rule from an EmailTemplate

Velocity Template Language (VTL) that we use to make the email notifications dynamic is very powerful, but it also takes a lot of effort and could sometimes take quite a bit of "trial and error" to get it all right. The syntax can be cumbersome and we do not always have the power that a programming language like Java or the BeanShell scripting language provides, especially if it comes to instantiating objects with parameters.

 

In a customer situation, I had to determine whether a work item was assigned directly to an identity (the owner), or that the original identity had a forward preference causing the work item to be assigned to the eventual owner.

 

To do so, I needed to instantiate the class sailpoint.api.Workflower and call the checkForward(...) method on that object. This turned out to be next to impossible. It is possible, however to get hold of an instance of the SailPointContext. Then using the SailPointContext, retrieve a Rule object and run that rule.

 

That means we can do even more magic! Rules can do just about anything! So, we can now more easily calculate things and return the outcome to a variable in the EmailTemplate.

 

So, how does this work? Well, first of all, we need to get hold of that SailPointContext. For that, see this blog post.

 

#set($spctx=$spTools.class.forName("sailpoint.api.SailPointFactory").getMethod("getFactory", null).invoke(null, null).getCurrentContext())

 

Then, we need to get the Rule object, for example "Example - My EmailTemplate Rule":

 

#set( $rule = $spctx.getObjectByName($spTools.class.forName("sailpoint.object.Rule"), "Example - My EmailTemplate Rule") )

 

Before we can run the rule, we need to set the rule argument as a List object. Let's assume that we just want the $identityName attribute to be provided as input to the rule. To add items to the list, we need to call the add() method on the List object. This however returns output, which would be printed in the template. To prevent that, we assign that output to a dummy variable.

 

#set( $ruleArgs=$spTools.class.forName("java.util.HashMap").newInstance() )
#set( $dummy = $ruleArgs.put( "identityName", $identityName ) )

 

Then we call the rule and assign the output of the rule to a new variable:

 

#set( $ruleOutput = $spctx.runRule($rule, $ruleArgs) )

 

The output of the rule can now be used in the template to make decisions, or to be part of the template.

 

Unfortunately, this trick will take a bit too many characters to do anything advanced in Subject, Cc and Bcc fields, but for the body, it's very useful.

Labels (3)
Version history
Revision #:
4 of 4
Last update:
‎May 16, 2026 01:15 PM
Updated by: