Most Powerful Open Source ERP

How To Use Notification Tool

How To showing how to send asynchronous messages from one user to one or many users.
  • Last Update:2016-02-09
  • Version:001
  • Language:en

The ERP5Notification tool provides a central point to send asynchronous messages from one user to one or many users. The purpose of the tool is to provide an API which is independent on how messages are actually going to be sent and when.

Table of Contents

  • Installation
  • How to send a notification
  • Related Articles

    Installation

    ERP5 is going to use ERP5Notification extensively. So, it is necessary to install it on systems before an upgrade. Since it is provided with the ERP5 product, you only need to go to the ZMI and add it by selecting 'Add ERP5 Tool'

    The Notification Tool will be (soon) automatically created with a new site.

    How to send a notification

    Here is an example about how to send a notification from a workflow transition:

    """
    This script tries to send a message to the appropriate recipient
    from the appropriate sender. It uses portal_notifications
    and the getObject API of ERP5Catalog.
    """
    from Products.ERP5Type.Log import log
    
    object = sci['object']
    translateString = context.Base_translateString
    portal_catalog = object.portal_catalog
    
    # Get the owner
    owner = object.getViewPermissionOwner()
    owner_value = portal_catalog.getResultValue(portal_type='Person', reference=owner)
    
    # Get the authenticated user
    user = context.portal_membership.getAuthenticatedMember().getUserName()
    user_value = portal_catalog.getResultValue(portal_type='Person', reference=user)
    
    # If users are not defined, we need to log and return
    if not owner or owner_value is None:
      # We keep a trace because this is the best we
      # can do (preventing answers is even worse)
      log("ERP5 Query Workflow", "No owner defined")
      return
    if not user or user_value is None:
      # We keep a trace because this is the best we
      # can do (preventing answers is even worse)
      log("ERP5 Query Workflow", "Current user is not defined")
      return
    
    # Build the message and translate it
    subject = translateString("Query was answered")
    msg = """The Query ID ${id} which you posted has been answered by ${user}
    
    Question:
    
    ${question}
    
    Answer:
    
    ${answer}
    """ 
    msg = translateString(msg, 
                 mapping=dict(id=object.getId(),
                              subject=subject,
                              user=user_value.getTitle(),
                              question=object.getDescription(),
                              answer=object.getTextContent())
                )
    
    # We can now notify the owner through the notification tool
    context.portal_notifications.sendMessage(sender=user, recipient=owner, subject=subject, message=msg)
    

    In this example, we provide user and owner as an object. However, it is also possible to provide them as a string or as a list (of object values or string). Future implementations may make unnecessary the validations which are made at the header of this script.

    Related Articles