Email Velocity Issue

I am getting the below issue:

sailpoint.tools.GeneralException: The specified class for ResourceManager (org.apache.velocity.runtime.resource.ResourceManagerImpl) does not implement org.apache.velocity.runtime.resource.ResourceManager; Velocity is not initialized correctly.
at sailpoint.tools.VelocityUtil.init(VelocityUtil.java:66)
at sailpoint.tools.VelocityUtil.secureRender(VelocityUtil.java:185)
at sailpoint.tools.MessageRenderer.render(MessageRenderer.java:108)
at sailpoint.tools.MessageRenderer.render(MessageRenderer.java:87)
at sailpoint.tools.MessageRenderer.secureRender(MessageRenderer.java:82)
at sailpoint.tools.MessageRenderer.secureRender(MessageRenderer.java:145)
at sailpoint.object.EmailTemplate.compile(EmailTemplate.java:267)
at sailpoint.server.InternalContext.sendEmailNotification(InternalContext.java:1051)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at bsh.Reflect.invokeMethod(Reflect.java:166)
at bsh.Reflect.invokeObjectMethod(Reflect.java:99)
at bsh.Name.invokeMethod(Name.java:858)
at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75)
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:102)
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:48)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:48)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:48)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:48)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHEnhancedForStatement.eval(BSHEnhancedForStatement.java:80)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:48)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:48)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHIfStatement.eval(BSHIfStatement.java:48)
at bsh.BSHBlock.evalBlock(BSHBlock.java:130)
at bsh.BSHBlock.eval(BSHBlock.java:80)
at bsh.BSHBlock.eval(BSHBlock.java:46)
at bsh.BSHTryStatement.eval(BSHTryStatement.java:88)
at bsh.Interpreter.eval(Interpreter.java:664)
at bsh.Interpreter.eval(Interpreter.java:758)
at bsh.Interpreter.eval(Interpreter.java:747)
at bsh.util.BeanShellBSFEngine.eval(BeanShellBSFEngine.java:186)
at org.apache.bsf.BSFManager$5.run(BSFManager.java:445)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)

where my logic is
/* ===== EMAIL LOGIC ===== */
String userEmail = identity.getEmail();
String managerEmail = null;
Identity manager = identity.getManager();
if (manager != null) {
managerEmail = manager.getEmail();
}
log.error("User Email : " + userEmail);
log.error("Manager Email : " + managerEmail);
if (!Util.isEmpty(userEmail)) {
EmailTemplate template = context.getObjectByName(EmailTemplate.class, "abc");
if (template != null) {
Map args = new HashMap();
args.put("identityName", identity.getDisplayName());
args.put("defaultPassword", password);
EmailOptions options = new EmailOptions(userEmail, args);
if (!Util.isEmpty(managerEmail)) {
options.setCc(managerEmail);
}
context.sendEmailNotification(template, options);
log.error("Password notification email sent successfully");
log.error("Primary AD password notification sent to user : "+ userEmail + " and manager : " + managerEmail);

} else {
log.error("Email template not found");
}
} else {
log.error("User email is empty");
}

Hi @Yashwanti ,

Please share the EmailTemplate that you are using in the above rule.

Thank You.

Your code calls context.sendEmailNotification(template, options) directly, which triggers VelocityUtil.init() inline. In BeanShell execution context, Velocity’s internal class resolution fails due to classloader isolation.

Replace:

context.sendEmailNotification(template, options);

to this:

Thread currentThread = Thread.currentThread();
ClassLoader originalClassLoader = currentThread.getContextClassLoader();
try {
    currentThread.setContextClassLoader(template.getClass().getClassLoader());
    context.sendEmailNotification(template, options);
} finally {
    currentThread.setContextClassLoader(originalClassLoader);
}

Hi,
Could you please let us know where you are getting this error ? By seeing this error we are not able to understand the issue. Could you please be more elaborative ?

@Yashwanti Where aer you calling this script?

Hello @Yashwanti, looking at the error statement you are not instantiating Velocity correctly. @naveenkumar3 above has provided you a great response and the solution to work your issue around.

Additionally within the code block, set a

try{}catch(error)finally{} statement, instead of a try{}finally{} , implement best practices, even though might take you a bit longer and specify the catch error in accordance to your business logic, please don’t set a standard nonsense.

I believe my response has helped you in solving this issue, please mark it as solution to help me back.

Hi @Yashwanti ,

Is this error occurring every time the “sendEmailNotification” method is called, in any Rule or Workflow in your environment?

Hi @Yashwanti , make sure once, EmailTemplate has correct syntax according to Apache Velocity . Its rendering issue , may be possible if conditions or any value passing to variable has syntax issue

Could you please specify where this code is written ?

Is issue fixed or still persisting ?