Regular Expression External Object
Beginning with the May 2013 release of m-Power, developers will be able to use a Regular Expression Java external object for parsing field values. The most common example of when this would be done is with the Email Upload template, in order to grab a piece of data from one section of the email. In the example below, we will be grabbing the Invoice Number from the email subject and storing it in the INVNO field.
- The first step is to create the proper regular expression and store it in an application calculation in the maintainer. Regular expressions are incredibly powerful and complex, and as such are well beyond the scope of this documentation. However, there is plenty of information online as well as tools to automatically generate regular expressions, such as this one, which is what was used for this example. If you want to test a regular expression you have created prior to using it, try our tool here. For this example, we are assuming the invoice number will come in the subject in the format "Invoice No. XXXXX". Therefore, our expression will be
/Invoice No\\. (\\d+)/and our calc will look like the below screenshot.
- Once you have added this expression as a calc, we need to register an associated external object. To do this, navigate to "Admin Menu" -> "Custom Code" -> "External Objects" -> "Create New Object." Give your object a name and description, and be sure the object type is set to Java Method. For the Class name, use the following:
For the method, use:
When completed, your object should look like the following screenshot.
- Next, we have to set up exactly three parameters. The first is the field you want to parse, the second is the regular expression to use, and the third is the field you want to return the result to. In our example, we will map the first parameter to the email subject, the second to the application calc we created in step 1, and the third to field INVNO.
- Now that our object has been created, we need to register it to our maintainer. To do so, click the Maintenance button, and then click the Edit button next to the desired maintainer. Once the Application Menu has been opened, click the External Object button. On this screen, select your object from the dropdown, the desired location (*BEFOREACT in this example), and then map the parameters to your fields, as discussed in the previous step.
Note: If your field does not appear in the parameter dropdown, you have incorrectly defined the length/decimal and this must be fixed before proceeding.
- Once added, compile/recompile this application to load in the new logic.
Note: You do not need to overwrite anything when only adding/modifying an external object.
Now, at runtime, the external object will check the value of the subject, parse the number, and store this parsed value in the database in the INVNO field for easy filtering/sorting.
Regular Expression Tester
|Calculation equal to:||"//"|
|String to parse:|