
Insert Record by Email Service Attachments in Salesforce
Email Service
Email service is the process that fulfills the business requirement by automated process service by using apex classes to process the content, attachment, and header of the inbound mail.
global class myHandler implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) { Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); return result; } }
There is a real-life problem which can be completed by the help of email service which is given as follows:
We have a .CSV file in which there are some Account records that you want to insert in the Salesforce. When we send an email with attached CSV to Salesforce by using the email service. After successfully emailing, the Account record is inserted from the CSV file.
Don't forget to check out: Email Services in Salesforce with Simple Example
Solution
STEP 1:
Create an email Service to get the mail id of the recipient to send with attached CSV file of the Account record that we want to insert in the database.
Add the apex class with this email service where all the functionality of the business requirements are written i.e. when we send an email to the recipient after the successful submission of the email account record is created in the Salesforce Org from the .CSV file.
Note: We have to create an email service every time for generating different email addresses to send multiple email messages for inserting records from different CSV files.
In the above image, we have an email service named InsertCSV in which Apex class: EmailServiceClass is embedded where we can find all the functionality to insert Account in the database.
STEP 2:
Create an Apex class: EmailServiceClass to insert the account from the CSV to the database by implementing the Messaging.InboundEmailHandler Interface which handles the inbound email and using handleInboundEmail method in that class we can get access to InboundEmail Objects to retrieve the contents, headers, and attachments of the inbound email message.
Create InboundEmailResult Object which returns the result of the Apex email message. InboundEmailResult object is used to return the result of the email service and if this object value is equal to null then the result is said to be successfully done. Separate every row of the CSV by splitting it each column twice.
Apex Class:EmailServiceClass
global class EmailServiceClass implements Messaging.InboundEmailHandler { global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,Messaging.InboundEnvelope envelope){ Messaging.InboundEmailResult result = new Messaging.InboundEmailresult(); // Messaging.InboundEmail.TextAttachment[] tAttachments = email.textAttachments; Messaging.InboundEmail.BinaryAttachment[] bAttachments = email.BinaryAttachments; String csvbody=''; String[] csvFileLines = new List<String>(); List<Account> accountlist= New List<Account>(); system.debug('bAttachments****'+ bAttachments); if(bAttachments !=null){ for(Messaging.InboundEmail.BinaryAttachment btt :bAttachments){ if(btt.filename.endsWith('.csv')){ csvbody = btt.body.toString(); system.debug(csvbody****'+ csvbody); //Now sepatate every row of the Csv csvFileLines = csvbody.split('\n'); system.debug('csvFileLines****'+ csvFileLines); for(Integer i=1; i < csvFileLines.size(); i++){ String[] csvRecordData = csvFileLines[i].split(''); Account accObj = new Account() ; system.debug(accObj****'+ accObj); accObj.name = csvRecordData[0] ; // accObj.accountnumber = csvRecordData[1]; // accObj.Type = csvRecordData[2]; // accObj.body = Blob.valueOf(tAttachment.Body); accountlist.add(accObj); } } } if(accountlist.size()>0) insert accountlist; system.debug('accountlist@@@'+ accountlist); } result.success = true; return result; } }
Check out another amazing blog by Aditya here: Users, Profiles, and Permission Set in Salesforce
STEP: 3
In the Above fig. We have an email address at which we have to send the attached CSV files. We got this email address during the creation of an email service through UI.
Reference: amitsalesforce.blogspot
Responses