2

I would like Salesforce to create some records for me based on data that is incoming in an email's CSV file attachment.

I've investigated the Email Services functionality, and set up an address, where if I send an email with a file attached, an Apex code runs. Now in this Apex code I would like to be able to verify the CSV file attachment, and if it's the correct type, go thru it, and create a custom record for each of the lines in the CSV file.

Thinking about it, I came down to the 2 following solutions, however, I'm unaware of the limitations of each method, so I would be incredibly grateful for any guidance!

VERSION 1: 

Inbound Email Service class takes care of all the CSV parsing, and record creating. Could there be any limits here on how much data can an Inbound Email Service class process?

or

VERSION 2:

Inbound Email Service class simply just creates a "JOB" type custom record where it also saves the attached CSV file from the email. (Could there be a size limit here?)

Then I have a separate process running on this JOB object type (which start upon creation), and I parse the CSV file at this time.

Any help is appreciated!

Later edit: forgot to mention, that the maximum size of the CSV is under 100 KB, and under 500 rows.

1 Answer 1

1

So the main limitation you will find here is the amount of Heap Space required of transforming your CSV into a workable Apex Type to parse through. That said as long as the combination of Rows and Columns in your CSV doesn't exceed that limit, either approach is fine. I actually did this as an AppExchange app a while ago and used the first approach.

EDIT (in response to your comment)

So I guess the first thing is just make sure when you convert your attachment from a blob to a string that it is actually right. Here is a sample Inbound Email parser:

global class MyEmailHandler implements Messaging.InboundEmailHandler
{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope)
    {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

        for(Messaging.InboundEmail.BinaryAttachment att : email.binaryAttachments)
        {
            if(att.fileName == 'testAtt.csv')
            {
                system.debug(att.body.toString());
            }
        }

        return result;
    }
}

Here is what my testAtt.csv looks like:

foo,bar
Hello,World

And here is what my debug() statement prints out:

|USER_DEBUG|[11]|DEBUG|foo,bar
hello,world

From there you should be able to take that string and put it through any CSV parser to give you a two dimensional array to parse through.

Sidenote; here are the settings for my Email Service, note the "Convert Text Attachments to Binary Attachments":

enter image description here

7
  • Hi Bryan! Thanks for the tip, I will go with the first approach, however, I'm facing another difficulty. When trying to parse the attachment's Body, I think the blob is not converted/read the right way. My CSV file is SUPER SIMPLE, there are no crazy things in it. Any idea on how to properly convert the attachment to a workable Apex Type, which I then can parse (maybe by using Marty Changs CSV reader?)
    – Laureant
    Commented Nov 18, 2019 at 11:41
  • @Laureant I updated my answer Commented Nov 18, 2019 at 14:31
  • Thanks for the update. Can I ask why the "Convert Text Attachments to Binary Attachments" is necessary? I didn't have that option selected...Could that be the source of all my problems? :)
    – Laureant
    Commented Nov 19, 2019 at 8:51
  • I did that because sometimes MIME types of attachments can be misinterpreted. So instead of having to build 2 methods, just always handle for binary attachments Commented Nov 19, 2019 at 11:48
  • That was the issue! Damn, I can't believe it. Forcing the attachment to binary, and then using the binary attachment's body (as a Blob) in the CSV Reader now works. Thank you very much. I thought that the .CSV is correctly interpreted as a text attachment (because it really is a text attachment).
    – Laureant
    Commented Nov 19, 2019 at 15:20

You must log in to answer this question.

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