Tips & Tricks - Saving an Email from Microsoft Exchange to a Data Group

This article provides a step-by-step guide on how to map an email from Microsoft Exchange to a record in an application and save it.

Create a connection

If you do not yet have a connection to Microsoft Exchange, first establish one using the Microsoft Exchange Connector. You can find instructions on how to do this here.

Import Microsoft Exchange Applications

If you haven't already done so, import the Exchange email application. Click here to see where to find the import package and additional information.

Customize the Exchange Application

Open the Exchange email app in the "Apps" module. Go to the "_tblPreview" page in the "Messages" data group. On this page, create a button and give it the title "Save Email." In the button's Properties dialog box, switch to the "Script" tab.

Click on " " and then "Edit JavaScript for Desktop."

Paste the following script into the JavaScript editor:

            function triggerWorkflow(p_strRecId)
{
    triggerUserWorkflowEvent('911F44EE227AE9D9D441C3CE57E10728A457F6E9', {qs_id: p_strRecId});
    return true;
}

        

This script is used to start a process that we'll create later. Use "p_strRecId" to pass the email ID to the process as a request parameter. Close the script editor by clicking "OK." On the "Script" tab, click " " "Add Script Call" and select the "onclick" event. In the "Function" field, enter the function call

            triggerWorkflow('$drRecord.getRecId()')
        

one. Confirm all dialog boxes by clicking "OK" and save the application.

Target Application

The email can be saved to a data group in any existing or newly created application by the process we'll create in the next chapter. To do this, a data field with the data type "File" is required in this data group. Create a data field like this in your target application. The field can then be included, for example, in a table that displays the records in the data group.

Create a process

Go to the "Processes" module and create a new process.

Remove any existing process elements and create a new generic event handler. In the Properties dialog, select the class "de.uplanet.lucy.server.workflow.eventhandler.UserWorkflowEventHandler" and use the GUID you used in the JavaScript function "triggerWorkflow()" (in this example, "911F44EE227AE9D9D441C3CE57E10728A457F6E9"). Confirm your entries by clicking OK.

Create a Groovy action and connect it to the Generic Event Handler. Enter the following script here:

            import de.uplanet.lucy.server.businesslogic.exchange.util.ExchangeUtils
import de.uplanet.lucy.server.businesslogic.exchange.util.ExchangeConnectionUtil
import java.text.SimpleDateFormat

def sysConn = g_dbConnections.systemConnection
def connEx = ExchangeConnectionUtil.getConnectionForWorkflowAction(g_context)
def messageUtil = ExchangeUtils.getMessageUtil(connEx)

def dtNow = now()
def sdf = new SimpleDateFormat("yyyy-MM-dd H-mm")
def dtNowForm = sdf.format(dtNow)

def strMessageId = g_request.get('qs_id')
def fileMail = new File(g_dirWorkflowTmp, "${dtNowForm}.eml")


ExchangeUtils.getMessageUtil().saveMessageAsEML(strMessageId, fileMail)
//messageUtil.saveMessageAsEML(strMessageId, fileMail)

def newId = newGuid()
def strSubject = g_dbQuery.executeAndGetScalarValue(connEx, "SELECT Subject FROM Message WHERE ID = ?"){
                            setString(1, strMessageId)
                         }

g_dbQuery.executeUpdate(sysConn, """INSERT INTO DATAGROUP('2CE389DFC3DDA6113B54C52A1C38907DB781910C')
									(STRID, DTINSERT, DTEDIT, LUSERID, LUSERIDINSERT, STR_TITLE)
									VALUES (?, ?, ?, ?, ?, ?)""")
{
	setString(1, newId)
	setTimestamp(2, dtNow)
	setTimestamp(3, dtNow)
	setInt(4, g_session?.user?.id)
	setInt(5, g_session?.user?.id)
	setString(6, strSubject)
}

g_dgFile.move(guid: "271DFA7E30C3329FBFB0572FB20150E74BC1B77A", id: newId, file: fileMail, name: "${dtNowForm}.eml",
    deleteAlways: true, triggerWorkflow: false)

        

In the SELECT and INSERT SQL statements, please replace the data group's GUID with the GUID of the data group from the target application. You can easily find the name in the editor by selecting your application in the "Application Structure" tab. Also, in the last line of the script, replace the GUID "271DFA7E30C3329FBFB0572FB20150E74BC1B77A" with the GUID of your file data field from the target application.

In the script, the desired email is temporarily stored in the first step so that it can then be inserted into the data group. Next, a new record is created in the data group using the "INSERT" SQL statement. The email's subject line is also used as the title of the new entry in the target application.

Please note that special characters in the subject line of an email may cause problems when saving it.

In the final step, "g_dgFile" is used to add the cached email object to the record that was just created. The following parameters must be specified:

  • g_context: The current processing context. "g_context" can be used as a constant.

  • fileMail: The file object of the cached email.

  • GUID: The GUID of the file data field in which the email is to be stored.

  • File name: The new file name of the saved file object.

  • true/false: Should a data group event handler defined on the target data group respond to the insertion of the file?

In this example, the current time is used as the name of the saved email. Other file names, such as "Subject" (without special characters), "Customer Number," or "ID," are also possible and can be chosen on a case-by-case basis. Save and publish the process, then open the email application in your browser. Select a message you want to archive and click the "Save Email" button. Once the email has been successfully saved, you'll find it in your target application.