1

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;
            }
        }    
    }
}
4
  • 5
    What is the problem? Commented Apr 8, 2016 at 0:45
  • 1
    @BorisBachovski, Perhaps that's his question? Why doesn't the above work? As in why have I have I included characters without specifying that they cannot be leading or trailing characters in my capture groups? Or why didn't I think to combine them differently so that couldn't happen? Gee, what should my regex look like?
    – crmprogdev
    Commented Apr 8, 2016 at 4:07
  • 2
    It'd be nice to have that explained before-hand and not to have to read through the code in order to understand the question. Commented Apr 8, 2016 at 4:11
  • Sorry Boris, i'll make sure i am more specific.
    – Slice32
    Commented Apr 13, 2016 at 1:10

2 Answers 2

1

This trigger works perfectly without any issues. I did a test with the following trigger (added debugs in the OP and omitted some fields) and a test class.

Trigger:

trigger ParseTrigger on Task (before insert, before update) {
    for (Task c: Trigger.new) {
        if (c.Description != null) {
            system.debug(c.Description);
            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()) {
                    system.debug('matches for: ' + bit);
                    foundEmailMatch = true;                        
                }
            }
        }    
    }
}

Test Class:

@isTest
public class parseTriggerTest {
    public static testmethod void test1() {
        Task task = new Task(
            Subject = 'test',
            Description = '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.'
        );
        Test.startTest();
        insert task;
        task = [select subject,description from Task where id=: task.Id];
        update task;
        Test.stopTest();
    }
}

Debug:

enter image description here

If you have no idea how to put this matched values into FROM and TO fields in your task, then use this trigger

trigger ParseTrigger on Task (before insert, before update) {
    for (Task c: Trigger.new) {
        if (c.Description != null) {
            system.debug(c.Description);
            Pattern emailPattern = Pattern.compile('(?i)^([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4})$');
            Boolean foundEmailMatch = false;
            Boolean isFrom = False;
            Boolean isTo = False;
            for (String bit: c.Description.split(' ')) {                    
                Matcher emailMatcher = emailPattern.matcher(bit);
                if (emailMatcher.matches()) {
                    if(isFrom){
                        system.debug('matches for: ' + bit + ' for From');
                        // Do your assignment to From field
                        isFrom = False;
                    }
                    else if(isTo){
                        system.debug('matches for: ' + bit + ' for To');
                        // Do your assignment to To field
                        isTo = False;
                    }                        
                    foundEmailMatch = true;                        
                }
                if(bit == 'From:')
                    isFrom = True;
                else if(bit == 'To:')
                    isTo = True;
            }
        }    
    }
}

Debug:

enter image description here

Hope it helps.

4
  • Am i missing something, it wont set both fields? if (emailMatcher.matches()) { if(isFrom){ system.debug('matches for: ' + bit + ' for From'); c.Parse_Email_From__c = bit; isFrom = False; } else if(isTo){ system.debug('matches for: ' + bit + ' for To'); c.Parse_Email__c = bit; isTo = False; }
    – Slice32
    Commented Apr 11, 2016 at 13:13
  • Did your debugs shows the bit as correct email? Any errors it throws in? Commented Apr 11, 2016 at 13:17
  • my debugs showed the email coming in correctly its only after assignments
    – Slice32
    Commented Apr 11, 2016 at 16:22
  • If the provided solution worked for you, upvote and accept the answer so that others with similar issue gets benefited. Thanks. Commented Apr 13, 2016 at 12:20
0

There is a salesforce plugin called "Noteninja - Deepsearch" that can help you search the Description field on Tasks and Events. I don't know if third-party plugins are off-limits for recommendations, but it basically allows you to index the blob field that SF won't allow you to.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .