Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Extract data from an email using velocity

Frederik De Roover
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2020

We're using JEMH, to intercept e-mails from users and extracting data from the e-mail to fill-in certain fields.

One of the things we've configured in JEMH is a script mapping rule, where we want to extract a certain part from an email and map this via Velocity into a custom field, which uses Insight data.

Example: From = role@vessel.domain.name

  • role@: this changes, and must be ignored
  • vessel: this is the part we want to extract
  • domain.name: this never changes, and must be ignored

So in the end we want vessel to remain as a value, so it can be mapped in the insight custom field.

A nice approach would be using regex, but we lack the java expertise to nail this one.

#set($regex = "")

#foreach ($from in $message.getHeader("From"))
$from.replace($regex, "")
#end

 

1 answer

1 accepted

1 vote
Answer accepted
Mike Harrison
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2020

Hi Frederik,

 

For setting a custom field based on a fixed set of email addresses I would use a project mapping domain rule, however it sounds like this sub-domain part is more dynamic/unpredictable.

Two different ways to approach this (choose one):

  1. Velocity driven custom field value. This sounds like what you have attempted so far.
  2. Set the custom field in your scripted mapping rule (this will only set the field on issue creation however)
  3. Set the custom field via Script Field Processor (this separates the setting of the field from the project mapping)

Velocity driven custom field value

A difficult one as we don't currently expose any regex utilities via this velocity script context. However, a (quite hacky) solution:

#foreach ($from in $message.getFrom())
#set ($fromAddress = $from.getAddress())
#if ($fromAddress)
$fromAddress.replaceAll(".+@","").replaceAll("\.domain\.name","")
#break
#end
#end

 

Setting field via script rule (issue creation only)

//you may have a match condition already, if so ignore this block
if (yourmatchcondition){
result.setMatch(true);
}

//this block will set a custom field with a regex extracted value like "vessel"
if (fromAddress && fromAddress.getAddress()){
var regex = /.+@([^\.]+)\.domain\.name/;
var address = fromAddress.getAddress();
var results = address.match(regex);
if (results && results.length > 1){
resultMap.put("your custom field name or id",results[1]);
}
}

 Note that both approaches require you to know how to/be able to set your custom field type with a basic string value.

 

Setting via Script Field Processor

if (fromAddress && fromAddress.getAddress()){
var regex = /.+@([^\.]+)\.domain\.name/;
var address = fromAddress.getAddress();
var results = address.match(regex);
if (results && results.length > 1){
resultMap.put("your custom field name or id",results[1]);
}
}

If you need further help, raise a support request with us (The Plugin People) .

Relevant links:

Frederik De Roover
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 14, 2021

Hi @Mike Harrison _The Plugin People_ using your guidance, we've been successful in implementing a couple off features using the code above.

Thank you very much!

Suggest an answer

Log in or Sign up to answer