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
Adam Hayden 6Adam Hayden 6 

Pulling extra information from an Email-to-Case Email.

Hi I wonder if anyone can help me.

I'm just starting out as a salesforce developer and I've been asked to make it so that when an Email-to-case email creates a new case, additional information is pulled from the body of the email and populates given fields of the case.

I decided to trying creating a Trigger Helper in APEX, to try to look for a prefix I know will be before each record in the email. I then want to extract the record information and pass it to the case. I havn't been able to find a whole lot about doing this on the internet and i'm wondering if i'm barking up the wrong tree?

I'm not really sure about how to interrogate the email that is related to the case in question, as the trigger I was writing will sit on the Case object how do I pull the Email body from the related email? I was assuming If I used an InsertAfterUpdate on the trigger, the case would exist with the related email saved for me to then read the body of and find the variables I needed.

Anyone with any advice, no matter how general is greatly appriciated. As I say, i'm just starting out so anything anyone can offer is hugely appriciated.

Many thanks,
Adam Hayden
pconpcon
You will have to do this as an after insert trigger so that the parent id is created.  The code below should give you the information you need to get started
 
trigger emailToCaseData on Case (after insert) {
    Set<Id> caseIds = new Set<Id>();

    for (Case c : Trigger.new) {
        if (c.Origin == 'Email') {
            caseIds.add(c.Id);
        }
    }

    if (!caseIds.isEmpty()) {
        Map<Id, EmailMessage> emailMap = new Map<Id, EmailMessage>();

        for (EmailMessage message = [
            select ParentId,
                FromAddress,
                HtmlBody,
                TextBody,
                Subject
            from EmailMessage
            where ParentId in :caseIds
        ]) {
            emailMap.put(message.parentId, message);
        }

        for (Case c : Trigger.new) {
            if (!emailMap.containsKey(c.Id)) {
                continue;
            }

            String subject = emailMap.get(c.Id).Subject;
        }
    }
}

You can see more about the data in the EmailMessage object here:

https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_emailmessage.htm
pconpcon
The code above also assumes that your Origin gets set to Email when it is created.