Using Commtemplate to send out email in custom Java Code

Hi Friends,

Consider the following scenario, you want to send out an email whenever the Owner Group field is changed on a workorder to everyone in the new owner group.

One option is to use MXServer.sendEMail with some hardcoded content or with just a few lines without any kind of formatting.

Better option is to design a Commtemplate and send out a very well formatted email with dynamic content.

There are just a few additional steps in code if you want to do that.

Step 1: Get the CommTemplate Mbo from MXserver

MboSetRemote commSetRemote = getMboSet(“$anylogicalnae”, “COMMTEMPLATE”, ” templateid=’YOUR_TEMPLATE’ “);

Step 2: Get the content:

if(commTemplateRemote.count() > 0)


CommTemplate commRemote = (CommTemplate)commTemplateRemote.getMbo(0);

// mboRemote should be that object whose content wil be replaced in commtemplate. So, if your commtemplate sends out workorder information nand you have used :wonum in your template make sure mboRemote is that object whose information will be resolved/replaced

SqlFormat sqf = new SqlFormat(mboRemote, commRemote.getString(“subject”));

//Set IgnoreUnresolved to true if you want to ignore any errors in commtemplate, errors for stuff like if you have used :wonum and it cannot resolve this attribute.


//Resolve Content method below converts template in to the dynamic content, by resolving I mean instead of :wonum it will put the current records wonum.

String subject = sqf.resolveContent();

//Similarly do it for the message cotent
sqf = new SqlFormat(mboRemote, commRemote.getString(“message”));


String message = sqf.resolveContent();

if (message.length() > 0)


                message = message + “rn”;


//Once you have resolved From, to everything else send out the email

MXServer.sendEMail(sendTO, null, null, sendFrom,subject, message, sendFrom, null, null);


A good combination can be if you also attach a report of the workorder with this email. For that you can read my post on attaching a birt report dynamically in java code.



12 thoughts on “Using Commtemplate to send out email in custom Java Code

  1. bobby

    Dear Sir,

    I’m beginner studying maximo.

    Please explain this line:

    CommTemplate commRemote = (CommTemplate)commTemplateRemote.getMbo(0);

    And, why do you use the number 0 (zero).

    And, what happen if the number is 1,2,3, …

    Thanks So Much!

      1. bobby

        Thanks Sir,
        And one more question. Please see the code below:

        MboRemote poline = getMboValue().getMbo();

        MboRemote prLineRemote = poline.getMboSet(“PRLINE”).getMbo(0);
        if (prLineRemote == null)
        poline.setValue(“prlinenum”, prLineRemote.getInt(“prlinenum”), 2L);

        poline.getMboSet(“PRLINE”) will get all PRline record with similar query as “select * from prline where prline.ponum = poline.ponum”. Or, poline.getMboSet(“PRLINE”) will get all PRline record follow relationship “PRLINE” ?

        And, poline.getMboSet(“PRLINE”).getMbo(0) will get the fist record. Is it right?
        How to know exactly which record we want to get?

        Thanks for your help
        Best Regards

      2. admin Post author

        Hi Bobby,

        You can filter the lines you get based on a specific linenum you want.

        For filtering you can setQbe or setWhere.

        getMbo(0) will get you the first record.

        If you have some specific criteria you can also create a relationship with that and use that instead of “PRLINE”.

        Hope this helps.

  2. Dave


    really useful info about the SqlFormat – but the bigger challenge is getting the recipients out of the communication template. Do you have some help for that?


  3. Dave

    I found the method to find the recipients. You can use this:

    String emailList = ((CommTemplateRemote)commRemote).convertSendTo(“COMMTMPLT_TO”, mboRemote);
    String sendTo = emailList;

    emailList = ((CommTemplateRemote)commRemote).convertSendTo(“COMMTMPLT_CC”, mboRemote);
    String cc = emailList;

    emailList = ((CommTemplateRemote)commRemote).convertSendTo(“COMMTMPLT_BCC”, mboRemote);
    String bcc = emailList;

    1. admin Post author


      Thanks for this information.

      The intention of my post was if you wanted to send adhoc emails with some formatting.

      If you want to send proper communication templates there is no need for all this.

      Just call “sendMessage(Mbo)” bethod on the commtemplate mbo and it will resolve everything and send the email.

  4. Dave

    I have read your post about sending reports as a PDF attachment. Would it be possible to add an attachment just using the sendMessage(Mbo) method, or is it in this case necessary to use the above method?


    1. admin Post author

      Hi Dave,

      If you are using sendMessage it will only send as per the commtemplate so if there is an attachment on the commtemplate it will send it but not attach anything dynamically.

      So, if you want to generate a report and attach you will have to use the above method and sendMessage cant help there.

      Or, even better option is override sendMessage like sendMessage(mbo, File[] attachments). That can be a good addition :).


  5. Martin

    Hi.. I’ve question about javascripts in Maximo.. I’d like to put on my SR application (SR and CreateSR) a Timer which show current time spent at working which open SR form. I should start in any opening of SR form form zero value and show runnong time. I know that I can put simple javascript like:

    <!– Hide from old browsers

    var hour = "0"
    var min = "00"
    var sec = "0"

    function timer(){

    if ((min < 10) && (min != "00")){
    dismin = "0" + min
    dismin = min

    dissec = (sec < 10) ? sec = "0" + sec : sec
    document.timer.counter.value = hour + ":" + dismin + ":" + dissec

    if (sec 59){
    min = “00”

    // –>

    But how can I put in on my SR form???? Colud You help me with it..

  6. Paul Pham

    My requirement is to send sms. I write action custom class to get message from communication template. The problem is, how can I write to communication log although I checked “write log” option in communication template? What functions should I call?

    1. Aniruddh Post author

      Hi Paul,

      If you are just using the communication template to get the formatted message and then in your java class sending email it won’t write to log even if you have checked the write log” checkbox.

      Instead of that use the commtemplate.sendMessage message this method will check if logflag is true then create the commlog entry for you.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s