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

how to create account records from inbound mail attachment data

Hi All,
Actually i need to get an email with attachment into salesforce using email services. The data in the attachment is a list of account names,phone,billingcity. Then after i want to create records on account object automatically based on the data in the attachment. I got how to receive an attachment using email service. And now how to populate records from attachment?

Here is my code (Email Service):-

global class ProcessContactApplicantEmail implements Messaging.InboundEmailHandler {

  global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope) {
     Account account;
     Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();

          try {
                 if ([select count() from Account where Name =:email.subject] == 0) {
                       account = new Account();
                       account.Name = email.subject;
                       insert account;
                 } else {
                       account = [select Id from Account where Name =:email.subject];
       for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
             Attachment attachment = new Attachment(); 
             attachment.Name = tAttachment.fileName;
             attachment.Body = Blob.valueOf(tAttachment.body);
             attachment.ParentId = account.Id;
             insert attachment;
       for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
             Attachment attachment = new Attachment();
           attachment.Name = bAttachment.fileName;
            attachment.Body = bAttachment.body;
             attachment.ParentId = account.Id;
             insert attachment;
       Note note = new Note();
       note.Title = email.fromName + ' (' + + ')';
       note.Body = email.plainTextBody;
       note.ParentId = account.Id;
       insert note;
      result.success = true;
     } catch (Exception e) {
           result.success = false;

    return result;

Hello Bittus,

You are trying to achive something which is not feasible in Salesforce. You can do this kind of data import. You have to go with somekind of integration tool (informatica, mulesoft) or scheduled data loaders (Command Line Interface of DataLoader) or import wizard.

Don't follow the way you are on as that is not possible. Hope this helps!

Hi Sumit, Is there any other way to do so? Like creating records with data present in attachment, that we get from inbound mail(i.e.,email service). I mean, without saving the inbound mail attachment in notes and attachments, we need to create records directly in desired standard/custom object. (Or) Am even trying to implement the File upload into sfdc concept with the inbound mail attachment.But, am not able to get this. Its not working and not even giving the result. Here is the code what i tried for this. Please have a look. Thnq. global class ContactEmailservice implements Messaging.InboundEmailHandler { public blob afile {set;get;} public string str; public string[] recs, d; public list ls = new list(); public list arecs {set;get;} global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope) { Account account; Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); try { if ([select count() from Account where Name =:email.subject] == 0) { account = new Account(); account.Name = email.subject; insert account; } else { account = [select Id from Account where Name =:email.subject]; } for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) { Attachment attachment = new Attachment(); attachment.Name = tAttachment.fileName; attachment.Body = Blob.valueOf(tAttachment.body); attachment.ParentId = account.Id; insert attachment; str = afile.toString(); recs = str.split('\n'); for(integer i=0; i
No, that is not feasible! You need to use integration tools for this kind of implementations where you can send .CSV to your server and that pushes it to Salesforce org.