Maximo

Maximo

Come for answers, stay for best practices. All we're missing is you.

 View Only
  • 1.  Sending email via comm template - dynamically replace the value of "recipient/s"?

    Posted Sat March 19, 2022 09:34 AM
    Hello,

    I have understood how to dynamically replace values of sendfrom, message, subject etc. via automation script. I have created a generic placeholder comm template that we would like to use for sending emails (instead of calling MXSever.sendEmail - which does not provide HTML formatting etc.).

    Using the automation script, I can change the values of sendfrom, message etc and invoke "sendMessage" method. However, how to specify to whom this email will be sent (the recipient/s of the email) to? How can I dynamically change it before invoking sendMessage method? 

    I tried using a NP attribute commtemplate.tolist. However it did not work.

    Any idea or suggestions?

    ------------------------------
    Pankaj Bhide
    Computer Systems Engineer
    Berkeley National Laboratory
    Berkeley CA
    ------------------------------

    #AssetandFacilitiesManagement
    #Maximo


  • 2.  RE: Sending email via comm template - dynamically replace the value of "recipient/s"?

    Posted Sat March 19, 2022 11:51 AM
    Hi Pankaj, nice to meet you in person this week.

    I've had some success doing this by creating a placeholder role and then overwriting it dynamically via script. The COMMTEMPLATE object has several relationships that could work depending on your needs (Child Object is COMMTMPLTSENDTO).

    In my particular use case I was setting the role to send to a dynamically selected person group (the GROUPNAME variable). Here's the code snippet:

        sendtoroleSet = mbo.getMboSet("COMMTMPLT_SENDTOROLE")
        sendtoroleSet.setWhere("sendtovalue='GLROUTEWF'")
        sendtoroleSet.reset()
        sendtorole = sendtoroleSet.getMbo(0)
        sendtorole.setValue("COMMTMPLTSENDTO_ROLE.VALUE", groupname, mbo.NOACCESSCHECK|mbo.NOVALIDATION|mbo.NOACTION)​


    ------------------------------
    Tim Ferrill
    Solutions Consultant
    Intelligent Technology Solutions
    tferrill@webuildits.com
    www.webuildits.com
    @tferrill/@webuildits
    ------------------------------



  • 3.  RE: Sending email via comm template - dynamically replace the value of "recipient/s"?

    Posted Sat March 19, 2022 01:35 PM
    Hello Tim,

    Thanks for your hint. I tried that. However it did n't work. Please see below:

    def send_email(email_to,email_from,email_subject,email_body):
        ctSet=MXServer.getMXServer().getMboSet("COMMTEMPLATE", mbo.getUserInfo())
        strWhere="templateid='LBL_GENERIC_EMAIL'"
        ctSet.setWhere(strWhere)
        ctSet.reset()
        if (not ctSet.isEmpty()):
            ctMbo=ctSet.getMbo(0)
            ctMbo.setValue("subject", email_subject, MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)
            ctMbo.setValue("sendfrom",email_from , MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)
            ctMbo.setValue("message", email_body, MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)
         
            ctTOSet=MXServer.getMXServer().getMboSet("COMMTMPLTSENDTO", mbo.getUserInfo())
            strWhere="templateid='LBL_GENERIC_EMAIL'"
            ctTOSet.reset()
            if (not ctTOSet.isEmpty()):
                ctTOMbo=ctTOSet.getMbo(0)
     
                ctTOMbo.setValue("sendtovalue", email_to, MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)
                ctTOMbo.setValue("type", 'EMAIL', MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)

            try: # since it could be a blocking call
               
                ctMbo.sendMessage(ctMbo, ctMbo)
            except:
                pass
        ctTOSet=None
        ctSet=None


    Is the sendMessage signature correct?

    And, yes, it was indeed my pleasure to meet you in person in Sacramento CA.  The user group conf was really informative and engaging.

    Pankaj Bhide





  • 4.  RE: Sending email via comm template - dynamically replace the value of "recipient/s"?

    Posted Sat March 19, 2022 01:47 PM
    Try changing this line:
            ctTOSet=MXServer.getMXServer().getMboSet("COMMTMPLTSENDTO", mbo.getUserInfo())


    To this:

            ctTOSet=ctMbo.getMboSet("COMMTMPLTSENDTO", mbo.getUserInfo())

    I suspect the issue is that because you are using MXServer to pull an unrelated set it's not including the updated address in your sendTo() call (because it's not part of the ctMbo Object).

    ------------------------------
    Tim Ferrill
    Solutions Consultant
    Intelligent Technology Solutions
    tferrill@webuildits.com
    www.webuildits.com
    @tferrill/@webuildits
    ------------------------------



  • 5.  RE: Sending email via comm template - dynamically replace the value of "recipient/s"?

    Posted Sat March 19, 2022 02:04 PM

    Pankaj,

     

    The to field is fetched dynamically from the commtemplate as part of the sendMessage method.  Specifically it takes the target Mbo, which in your case is the comm template itself and then resolves the send to based on the COMMTMPLT_TO relationship from the template to the COMMTMPLTSENDTO table.  From there it finds the role, person or email to send the email to and uses that.

     

    Assuming you want to send only to a specific user you could use temporarily add a record to the COMMTMPLTSENDTO object by the COMMTMPLT_TO relationship.

     

    So something like this:

     

    def send_email(email_to,email_from,email_subject,email_body):

        ctSet=MXServer.getMXServer().getMboSet("COMMTEMPLATE", mbo.getUserInfo())

        strWhere="templateid='LBL_GENERIC_EMAIL'"

        ctSet.setWhere(strWhere)

        ctSet.reset()

        if (not ctSet.isEmpty()):

            ctMbo=ctSet.getMbo(0)

            ctMbo.setValue("subject", email_subject, MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)

            ctMbo.setValue("sendfrom",email_from , MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)

            ctMbo.setValue("message", email_body, MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)

        

            ctTOSet = ctMbo.getMboSet("COMMTMPLT_TO")

            toemail = ctTOSet.add()

            toemail.setValue("TYPE", "EMAIL")

            toemail.setValue("SENDTO", True)

            toemail.setValue("SENDTOVALUE", email_to)

     

            # Do not save since we just want it to be temporary

     

            try: # since it could be a blocking call            

                ctMbo.sendMessage(ctMbo, ctMbo)

            except:

                pass

        ctTOSet=None

        ctSet=None

     

     

    Note that you should close the MboSet not just set them to None and that should be in a finally block like this so that the MboSet is closed everytime.

     

    def send_email(email_to,email_from,email_subject,email_body):   

        ctSet=MXServer.getMXServer().getMboSet("COMMTEMPLATE", mbo.getUserInfo())

        try:

            strWhere="templateid='LBL_GENERIC_EMAIL'"

            ctSet.setWhere(strWhere)

            ctSet.reset()

            if (not ctSet.isEmpty()):

                ctMbo=ctSet.getMbo(0)

                ctMbo.setValue("subject", email_subject, MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)

                ctMbo.setValue("sendfrom",email_from , MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)

                ctMbo.setValue("message", email_body, MboConstants.NOACCESSCHECK | MboConstants.NOVALIDATION_AND_NOACTION)

           

                ctTOSet=ctMbo.getMboSet("COMMTMPLT_TO")

                toemail = ctTOSet.add()

                toemail.setValue("TYPE", "EMAIL")

                toemail.setValue("SENDTO", True)

                toemail.setValue("SENDTOVALUE", email_to)

     

                # Do not save since we just want it to be temporary

     

                try: # since it could be a blocking call            

                    ctMbo.sendMessage(ctMbo, ctMbo)

                except:

                    pass   

        finally:

            if not ctSet is None:

                ctSet.close()

       

        ctSet=None

     

    Also you have a comment about using a try / catch because the call may be blocking.  Be aware that a try / catch is not going to stop the blocking call, you would need to create a new thread if that is the intent.

     

    Jason

     

     

     

    Jason VenHuizen

     

    +1-206-669-6430 | sharptree.io

     






  • 6.  RE: Sending email via comm template - dynamically replace the value of "recipient/s"?

    Posted Sat March 19, 2022 09:14 PM
    Thanks again Tim for your help.

    Jason - About try/catch - I thought that the sendEmail may fail if there could be any issues with SMTP (beyond MAXIMO's control) relay. I therefore put it in try/catch assuming that if the sendEmail fails, it will simply pass without throwing an exception which will have no impact  on the transactions from where I am calling this method. Can you please clarify if that is needed or not or anything more is required ? Thanks for your help.

    By the way, I am now able to send the email via the comm template.