+ Start a Discussion
Ishan.AroraIshan.Arora 

i want to insert record xls file using email handler. Any ideas?

i want to insert record xls file using email handler. Any ideas?
NagendraNagendra (Salesforce Developers) 
Hi Ishan,

If I understand your requirement correctly An excel file could be difficult as it is a binary object, and there is no internal apex method to decode it. If you had an excel based CSV file, that is a different story.

1) You could set up an inbound email service with a specific address to send CSV files to. (look at documentation on Inbound email)

2) you could write code to extract the attachment from the message for processing (you wouldn't need to save it as a document first, that would be an extracting step). (look at documentation for binary email attachments and converting blob to string)

3) you would need to write a parser to extract the rows out of the CSV file, don't forget to account for things like names with commas in them, they can reek havoc on a parser. (this one will take some effort, depending on the complexity of the data, could be a simple string split, could require much more)

4) write code to insert or upsert the extracted records to your custom object. (create an array list of your custom object and new instances of your custom object for each record you parse get loaded to it, then insert or upsert the list in batch).

For more information please check with below link. Hope this helps.

Please mark this as solved if the information helps so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra
 
Parteek Goyal 3Parteek Goyal 3
Hi Ishan,

Please refer below code.
trigger insertAttachmentRecordtoObjectonCase on case (after update) {
    
    public Blob csvFileBody{get;set;}
    public string csvAsString{get;set;}
    public String[] csvFileLines = new String[]{};
    string[] csvHeaders;
    public List<account> acclist = New List<Account>(); 
    Map<String,Integer> mapfileHeaders = new Map<String,Integer>();
    ID caseid;
    String strHeader;
    
    if(trigger.isupdate){
    if(checkRecursiveinsertAttachment.runonce()){

    //   try{
         //  csvAsString = csvFileBody.toString();
         //  csvFileLines = csvAsString.split('\n'); 
         for(case case1 : trigger.new){
             caseid = case1.id;
         }
         
         System.debug(caseid);
         
        EmailMessage Em = [Select ID,ParentID from EmailMessage where ParentId =:caseid AND HasAttachment = true AND Incoming = True];
        System.debug(Em);
        Attachment att=   [Select Id,ParentId, Name,body,ContentType From Attachment where ParentID =:Em.id limit 1]; 
        System.debug(att);
        String[] filelines = new String[]{};
        string nameFile;
        nameFile=att.body.toString();        
        filelines = nameFile.split('\n');    
           String[] fileHeaders;
           System.debug(filelines);
           System.debug(filelines.size());
           for(Integer i=0;i < 1;i++){
              
              fileHeaders = filelines[i].split(',');
           }
           System.debug(fileHeaders);
           System.debug('Test Debug 1');
           for(Integer i=1;i < filelines.size();i++){
               csvHeaders = filelines[i].split(',');
           }
           System.debug(csvHeaders.size());
           for(Integer i=0;i < csvHeaders.size();i++){ 
             //  System.debug(fileHeaders[i]); 
               mapfileHeaders.put(fileHeaders[i],i);    
           }
          System.debug(mapfileHeaders);
          
           for(Integer i=1;i < filelines.size();i++){
               Account accObj = new Account();
               string[] csvRecordData = filelines[i].split(',');
                   System.debug(mapfileHeaders.values());
                   System.debug(mapfileHeaders.get('Type'));
                   if(mapfileHeaders.containsKey('Type')){
                       integer var3 = mapfileHeaders.get('Type');
                       System.debug(csvRecordData[var3]);
                       accObj.Type = csvRecordData[var3];  
                   }
                   if(mapfileHeaders.containsKey('AccountSource')){
                       Integer var4 = mapfileHeaders.get('AccountSource');
                       accObj.AccountSource = csvRecordData[var4];      
                   }
                   if(mapfileHeaders.containsKey('Accountnumber')){
                       integer var1 = mapfileHeaders.get('Accountnumber');
                       accObj.accountnumber = csvRecordData[var1];  
                   }
                   
                   if(mapfileHeaders.containsKey('Company Name')){
                       Integer var2 = mapfileHeaders.get('Company Name');
                       accObj.name = csvRecordData[var2];      
                   }
                   if(mapfileHeaders.containsKey('Industry')){
                       Integer var5 = mapfileHeaders.get('Industry');
                       accObj.Industry = csvRecordData[var5];      
                   }
                   if(mapfileHeaders.containsKey('Phone')){
                       Integer var6 = mapfileHeaders.get('Phone');
                       accObj.Phone = csvRecordData[var6];      
                   }
                   if(mapfileHeaders.containsKey('NumberOfEmployees')){
                       Integer var7 = mapfileHeaders.get('NumberOfEmployees');
                      // accObj.NumberOfEmployees = csvRecordData[var7];   
                   }
               acclist.add(accObj);   
              
          
           }
          insert acclist;
     /*   }
        catch (Exception e)
        {
          
        }  */
    }
    }
}

Thanks,
Parteek​