function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ellsa JamesEllsa James 

Formula to pull an email address from a text area field

I am trying to pull an email address from the standard "Description" field on Cases object. The below formula is correctly pulling everything AFTER the "@" symbol in the email address, (for example "@sample.com").I cannot get it to pull the first part of the email address.

Here is the formula I am using
SUBSTITUTE( Description ,RIGHT(Description, FIND("@",Description))+ LEFT(Description, FIND("@",Description)), NULL)

All help much appreciated.
pconpcon
I do not think you can do this correctly in a formula field.  I think your best bet would be to do this via a trigger and store the value into a test field.  I gave it a shot, but the regex isn't working for all instances
 
for (Case c: Trigger.new) {
    if (c.Description != null) {
        Pattern emailPattern = Pattern.compile('(?i).*\\s([A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4})\\s?');
        Matcher emailMatcher = emailPattern.matcher(c.Description);
        if (emailMatcher.matches()) {
            c.Description_Eamil__c = emailMatcher.group(1);
        } else {
            c.Description_Eamil__c = null;
        }
    }
}

or you could use a much less effecient method like:
 
trigger CaseTrigger on Case (before insert, before update) {
    for (Case 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.SLoggly__Description_Eamil__c = bit;
                }
            }
            
            if (!foundEmailMatch) {
                c.SLoggly__Description_Eamil__c = null;
            }
        }
    }
}

 
Mikola SenykMikola Senyk
Are you trying to extract email from text field that looks like "My email is info@sample.com, for real offers only"?
If it is true I am afraid it is not possible via formula field.
You need to use something similar to text field and to update it from trigger.