Friday, December 2, 2016

Reading Data from Text file.

Example: This scenario is for inserting a file of email ids at a time by reading from a text file. Here we used apex:inputfile for uploading a file.

Note: In this scenario all the email ids of text file must be separated by Comma.

Page:

<apex:page sidebar=”false” controller=”FileReadingClassxxx”>
    <apex:form >
         <apex:inputfile value=”{!fileBody}”/>
              <apex:commandButton value=”ReadDataIntoMyObject” action=”{!readContent}”/>
    </apex:form>
</apex:page>


Controller:

public with sharing class FileReadingClassxxx {
  String fileContent=' ';
  public blob fileBody { get; set; }
  public PageReference readContent() {
       fileContent = fileBody.toString();
       List<string> allemails = fileContent.split(',');
       EmailSpace__c myemail = new EmailSpace__c();
       List<EmailSpace__c> lstemails = new List<EmailSpace__c>();
     
       for(Integer i=0;i<allemails.size();i++){
           myemail = new EmailSpace__c(name=allemails[i]);
          lstemails.add(myemail);
      }
      insert lstemails;
      return null;
  }
}

No comments:

Post a Comment