html code

Creating Salesforce Records via Email Service Attachments

Email Handler Service

The email service entails an automated process that meets business needs by employing Apex classes to handle the content, attachments, and headers of incoming emails.

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

A practical scenario benefiting from an email service is outlined below:

Suppose there’s a .CSV file containing Account records to be inserted into Salesforce. Using the email service, when an email with the CSV attached is sent to Salesforce, the system processes the email, successfully extracting and inserting the Account records from the CSV file.



Create an email service to retrieve the recipient’s email address and send an attached CSV file containing the Account records intended for database insertion.

Incorporate an Apex class within this email service, housing the entire functionality specified by the business requirements. This includes the process where, upon successful submission of an email, Account records are created in the Salesforce Org from the associated CSV file.

Note: A distinct email service needs creation each time to generate different email addresses for sending multiple email messages aimed at inserting records from various CSV files.

In the provided image, there exists an email service named “InsertCSV,” incorporating the embedded Apex class: EmailServiceClass. This class contains all the necessary functionality to execute the insertion of Account records into the database.


Develop an Apex class named “EmailServiceClass” to facilitate the insertion of accounts from the CSV into the database. Implement the Messaging.InboundEmailHandler Interface within this class, enabling it to manage inbound emails. Through the handleInboundEmail method within this class, access is granted to the InboundEmail Objects, allowing retrieval of the inbound email message’s contents, headers, and attachments.

Instantiate an InboundEmailResult Object, responsible for conveying the outcome of the Apex email message. The InboundEmailResult object serves as a conduit for the email service’s result, and if its value equates to null, the operation is considered successful. Each CSV row is segregated by employing a double-column split method for effective separation of data.

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){
                    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);   
               = csvRecordData[0] ;
                        //  accObj.accountnumber = csvRecordData[1];
                        // accObj.Type = csvRecordData[2];     
                       // accObj.body = Blob.valueOf(tAttachment.Body);                 
                insert accountlist;
            system.debug('accountlist@@@'+ accountlist);
        result.success = true;
        return result;        


In the depicted image, we can observe the designated email address to which we must send the attached CSV files. This email address was acquired during the email service creation process using the user interface.

Reference: amitsalesforce.blogspot