function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SeraphSeraph 

N>Batch Version of this Code

I am currently following this link
http://www.forcetree.com/2010/08/read-and-insert-records-from-csv-file.html
and I tested it on my developer account. It worked great but when I tried to insert a large csv data (3000 rows) it had an error of regex too complicated. I know that the solution is by using batch apex http://developer.financialforce.com/customizations/importing-large-csv-files-via-batch-apex/ . the problem is I can't implement it on the forcetree code because I can't follow most of the explanation since I'm new in apex.
Can anyone pls share how to implement batch apex to the forcetree sample code? THX
SeraphSeraph
here's the code
controller(FileUploader)------------------------------------------------------------------
public class FileUploader 
{
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<Account> accstoupload;
    
    public Pagereference ReadFile()
    {
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        accstoupload = new List<Account>();
        for (Integer i=1;i<filelines.size();i++)
        {
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
            
            Account a = new Account();
            a.Name = inputvalues[0];
            a.ShippingStreet = inputvalues[1];       
            a.ShippingCity = inputvalues[2];
            a.ShippingState = inputvalues[3];
            a.ShippingPostalCode = inputvalues[4];
            a.ShippingCountry = inputvalues[5];

            accstoupload.add(a);
        }
        try{
        insert accstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    public List<Account> getuploadedAccounts()
    {
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
                return accstoupload;
            else
                return null;                    
        else
            return null;
    }            
}
VF page (UploadAccounts)-------------------------------------------------------------------
<apex:page sidebar="false" controller="FileUploader">
   <apex:form >
      <apex:sectionHeader title="Upload data from CSV file"/>
      <apex:pagemessages />
      <apex:pageBlock >
             <center>
              <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/>
              <br/> <br/> <font color="red"> <b>Note: Please use the standard template to upload Accounts. <a href="{!URLFOR($Resource.AccountUploadTemplate)}" target="_blank"> Click here </a> to download the template. </b> </font>
             </center>  
      
      
      <apex:pageblocktable value="{!uploadedAccounts}" var="acc" rendered="{!NOT(ISNULL(uploadedAccounts))}">
          <apex:column headerValue="Account Name">
              <apex:outputField value="{!acc.Name}"/>
          </apex:column>
          <apex:column headerValue="Shipping Street">
              <apex:outputField value="{!acc.ShippingStreet}"/>
          </apex:column>
          <apex:column headerValue="Shipping City">
              <apex:outputField value="{!acc.ShippingCity}"/>
          </apex:column>
          <apex:column headerValue="Shipping State">
              <apex:outputField value="{!acc.ShippingState}"/>
          </apex:column>
          <apex:column headerValue="Shipping Postal Code">
              <apex:outputField value="{!acc.ShippingPostalCode}"/>
          </apex:column>
          <apex:column headerValue="Shipping Country">
              <apex:outputField value="{!acc.ShippingCountry}"/>
          </apex:column>
      </apex:pageblocktable> 
      
      </apex:pageBlock>       
   </apex:form>   
</apex:page>

pls help me...im stuck
James LoghryJames Loghry
I've attempted to implement similar requirements before, and it honestly never ends well.  Apex just isn't the best solution to parse csv's and then to upload them into Salesforce.  Here's some tips for getting close though:
  • Have one regex that breaks out your csv file into large chunks of strings
  • Iterate through those strings to do the smaller regex's to avoid those regex too complicated errors.
  • Use apex batching to process more than 10k records as you know
  • Use something similar to this for displaying the progress of the upload http://www.tehnrd.com/batch-apex-status-bar/
  • Be careful with your view state size (this was a solution killer for me), as you will run into view state size limits easily with large files.
SeraphSeraph
Thx @James Loghry for such good explanations,,,when you said  Apex just isn't the best solution to parse csv's and then to upload them into Salesforce, what possible alternatives can I use or you can suggest based on you experience? I have read about pattern and matcher..will it do any better?