Keyword based routing


Preliminary Notes

The "Receive Action" script contains key objects that are essential for the script to run and are read/written by the script.

  • task: The ticket object that you can update as needed, depending on your inbound email requirements.
  • message: The email from which the ticket was created. When the script is executed, the ticket (task) has already been read and created accordingly using the mailbox configuration defined in "Base Data".
  • coretext: This string is a summary of all text parts of the message. An inbound email can contain various parts of text, plain text or HTML-formatted text parts. coretext contains all of these parts and can therefore be ideally used to search for keywords. There is therefore no need to check all the different MIME parts of the email. 
  • helper: This is a helper object that provides a variety of useful functions such as redirecting a ticket; We will use this functionality below.

What to do

Depending on your business processes, it may generally be necessary to define the internal distribution of centrally incoming emails more precisely using keywords. These keywords can be read from both the subject, the address lines and the message body.

To do this, please start todo4teams and log in as “Admin” or “Superadmin”, depending on your user rights. Only as a user with administrator rights can you edit and manage mailboxes.

Go to the Mailboxes navigation point  menu_mailboxes_en.pngand click on the email address you want to configure in a row. The configuration dialog for editing mailboxes opens automatically.

In the Scripts tab, select the “Receive Action” to create the corresponding script.

The Script

With the set of variables mentioned above, it is quite easy to search an incoming email for keywords. We now want to understand how a message containing the word "salesforce" is treated in a special way. Emails containing this keyword should be treated differently from the settings configured in "Base Data".

The following part of the script code will add the title "Salesforce" to the ticket and distribute the message to a group called "Salesforce Experts":


if(coretext.toLowerCase().indexOf("salesforce")>-1) {  
 task.title="Salesforce: "+task.title;    
 helper.routeToGroupByName("Salesforce Experts");

}

The script converts the contents of the coretext so that the word "Salesforce" can then be searched for using the indexOf function. indexOf will output the location of "Salesforce" or the value -1 if the keyword does not appear in the message. The code within the curly brackets is only executed if "Salesforce" was found in the text.

The helper method routeToGroupByName will change the distribution of the ticket and pass it on to the group named "Salesforce Experts". To ensure that the script works, a team with this name must exist and be marked as active in the basic settings. Alternatively, the routeToGroup method can be used, which has the advantage that it addresses the ID of the team and is therefore less sensitive to the renaming of teams or similar. If the team "Salesforce Experts" has the id 17, the followig command is equivalent to the routeToGroupbyName command:

helper.routeToGroup(17);

You can use the above script as a template to define your own rules. To do this, copy the four-line code into the "Receive Action" and change the keywords and team names to suit your needs. After saving, you can test the rules by sending emails with the corresponding criteria.

If you only want to search the subject of an email for keywords, the following script would be sufficient:

if(message.getSubject().indexOf("Alarm:")>-1 ||
   message.getSubject().indexOf("Emergency:")>-1 ||
   message.getSubject().indexOf("Important:")>-1 ){
    task.title="Alarm: "+task.title;
   // ... do something special with this type of message
}

In this case, the subject is searched for "Alarm", "Emergency" and "Important" and the results are filtered using an OR connection. In Javascript, this is achieved using the expression "||".