I am trying to parse out both TO: and FROM: Emails from the description field on a task and put them into two fields.
Example of the text From: [email protected]
To: [email protected] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis vulputate elit. Cras erat ex, sagittis et dolor sed, condimentum sagittis elit. In vel arcu efficitur, vulputate odio sit amet, egestas dolor. Nullam porttitor porta pretium. Donec vulputate ex et magna maximus tristique. Cras eu felis nec felis viverra laoreet a eu tellus. Sed id nunc est. Suspendisse ornare lorem augue, ut commodo dui tempor vel. Cras risus mi, ornare quis fringilla in, laoreet non nunc. Integer elementum malesuada nisl, id maximus mi volutpat eu. Suspendisse laoreet sem erat, vel placerat sapien interdum sed. Morbi pharetra blandit massa, varius porttitor nibh vestibulum sit amet. Proin maximus lacus eget ante fermentum venenatis.
Sample code:
trigger ParseTrigger on Task (before insert, before update) {
for (Task c: Trigger.new) {
if (c.Description != null) {
Pattern emailPattern = Pattern.compile('(?i)^([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4})$');
Boolean foundEmailMatch = false;
for (String bit: c.Description.split(' ')) {
Matcher emailMatcher = emailPattern.matcher(bit);
if (emailMatcher.matches()) {
foundEmailMatch = true;
c.To_Email__c = bit;
}
}
if (!foundEmailMatch) {
c.To_Email__c = null;
}
}
}
}