Send Communication Template from script

This is a quite old topic that has been already addressed in many Maximo communities and blogs around. However, I have found none of them seems to handle it correctly.

There are mainly several techniques of sending an email from an automation script. I will explain the limitations of the current solutions and propose a best practice.

If you just want to se the final solution jump here.

Using Java APIs – worst solution

You know you can use any Java API in your Maximo automation scrips so the first solution is to use the javax.mail package to send email.

This is definitely the worst solution since it is totally non-standard in Maximo and you have to deal with authentication, encoding, SSL, etc. You also have to get the SMTP settings from Maximo properties as well ending up with a lot of code to write and maintain.

Using MXServer.sendEMail() method – minimal solution

Second attempt is to use the sendEMail method of the MXServer class. The script will look like this.

MXServer.getMXServer().sendEMail(_to, cc, bcc, from, subject, message, replyto, files, urls)

This is better than using the Java APIs but you are still not using a Communication Template so you have to build the subject and body of your email in the script itself. For example, you cannot use the nice variable substitution feature available in the Communication Templates.

Using CommTemplate object (good enough)

This example is using the CommTemplate object.

ctMboSet = mbo.getMboSet("$commtemp", "COMMTEMPLATE",  "TEMPLATEID ='MYCOMMTEMPLATE'")
ctMbo = ctMboSet.getMbo(0)
ctMbo.sendMessage(mbo, mbo)

This technique has one major flaw. It is not saving the message to the communication log that is visible in the typically visible in the Log tab of the application.

Using CommLog object (best practice)

Here we are leveraging the CommLog object that Maximo is using when you select the Create > Communication action from the menu of the Work Order Tracking application for example.

By using the following three lines of code you are exactly replicating what you are doing when sending a communication from the user interface.

clMbo = mbo.createComm()

This approach is perfectly leveraging the Communication Template features and is also saving the message to the communication log.

You have to be aware that this is a ‘blocking’ call so in case of errors if your SMTP server is not available or there is an error the user will be blocked. If you want to better manage errors you may wrap the call in a try/except setting the ISSENDFAIL flag so it can be retried later using the technique described in this post.

clMbo = mbo.createComm()
  clMbo.setValue("ISSENDFAIL", True, MboConstants.NOACCESSCHECK)
Send Communication Template from script

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top