+ Start a Discussion

How to create records from the data in attachment sent by inbound email service

Hi all,

Actually i need to generate records in account object based on the data present in the attached file, which wll be sent to Salesforce through email. I am trying to do so using Inbound Email Service, but am only able to read the body of the email but not the attachment data.
Ajay K DubediAjay K Dubedi
Hey Bittus,
For doing this  first of all you have to save your attachment as record and once your attachment saved in any record (if the attachment is in csv format)
you have to parse that record and store each line of data in  a list so when you are able to store those data in form of list you just have to do iteration  over those  
lists and insert each record in account object according to their index values and mappings.
Can u please provide an example for this? This is my email service class 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 + ' (' + DateTime.now() + ')'; note.Body = email.plainTextBody; note.ParentId = account.Id; insert note; result.success = true; } catch (Exception e) { result.success = false; } And in csv file, i have data like below Ram,123,Hyd Robert,454455,BAngg Rahim,4545,Kerala I need to get this records saved in account object. Can u edit my email service *thanks & regards* *nanda kishore*
Hi All, From the Email service which i posted recently displays only the body of the inbound mail. Here is the updated code which helps to see the data in attachment. But am not able to insert these records/data into contact object. These records should be inserted or generated automatically into contact object without using any tool. Here is the code:- global class ProcessJobApplicantEmail implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); Contact contact = new Contact(); contact.FirstName = email.fromname.substring(0,email.fromname.indexOf(' ')); contact.LastName = email.fromname.substring(email.fromname.indexOf(' ')); contact.Email = envelope.fromAddress; insert contact; System.debug('====> Created contact '+contact.Id); if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) { for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) { Attachment attachment = new Attachment(); // attach to the newly created contact record attachment.ParentId = contact.Id; attachment.Name = email.binaryAttachments[i].filename; attachment.Body = email.binaryAttachments[i].body; insert attachment; } } return result; } } Can anyone edit my code inorder to meet the requirement *thanks & regards* *nanda kishore*