2

If I upload a csv file and I want to insert that csv file data s in to Account object, I create a code but i got some errors. If I load 2 columns in csv and add 5 columns in apex class but i got a exception Array Index Out of Bound Exception

Apex class

public class FileUploader
{
  String[] filelines = new String[]{};
  public List<Account> contoupload;
  public Blob contentFile { get; set; }
  public String nameFile { get; set; }

  public PageReference ReadFile()
  {
    System.Debug('Entry123');
    nameFile=contentFile.toString();
    System.Debug('nameFile123'+nameFile);
    filelines = nameFile.split('\n');
    contoupload = new List<Account>();
    for(Integer i=1;i<filelines.size();i++)
    {
        System.Debug('Values12313'+filelines.size());
        String[] inputvalues = new String[]{};
        inputvalues = filelines[i].split(','); 

            Account a = new Account();
            a.Name = inputvalues[0]; 
            System.Debug('InputValues'+a.Name);
            a.Phone = input values[1]; 
            a.Shipping Street = inputvalues[2];       
            a.ShippingCity = inputvalues[3];
            a.ShippingState = inputvalues[4];
            a.ShippingPostalCode = inputvalues[5];
            a.ShippingCountry = inputvalues[6];
            contoupload.add(a);
            System.Debug('Account Entry'+contoupload.size());

    }
    try
    {
     if(contoupload.size()>0)
     {
       insert contoupload;
       System.Debug('FileSize12311'+contoupload.size());
     }
    }
    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);
    } 
     PageReference pg = new PageReference('https://csesparks-dev-ed--pack-saleforce.ap1.visual.force.com/apex/Fileupload');
     pg.setRedirect(true);
     return pg;
 }    
}

Error like this:

System.ListException: List index out of bounds: 2

  • 1
    Can you post an example of your CSV file? On the face of it, it appears simply because your inputvalues array is reflecting the true number of columns in your CSV. Where you expecting it to have 5? – Andrew Fawcett Apr 15 '13 at 14:51
  • 1
    Also, depending on the source of your CSV file or the likelyhood of quotes in the data values etc, you may want to consider enhancing your CSV parsing logic a little. Or utilise the library here. frombelvideres4thfloor.blogspot.com.es/2010/10/… – Andrew Fawcett Apr 15 '13 at 14:53
  • Thanks for your comment Andrew Fawcett...I am having a csv file column as Name,Phone and in apex class i added 5 columns.As my point of view is it possible to do like added 2 or 3 columns in excel sheet and add 5array values in apex class?And it is add in to the SObject or any other way to do this program........ – Venkatsforce Apr 16 '13 at 6:09
2

Given your current code, the number of columns in your file needs to match the number of array items your code is expecting in the 'inputvalues' array.

Looking at the code you have provided, you need to provide 7 columns on each row in the file. If you don't, you will get the List index out of bounds error.

So you cannot just provide the Name and Phone in the CSV file, you need to provide...

Fred Blogs,+44123456789,Fred Street,Fred City,Fred State,Fred Postalcode,Fred Country

Or enhance your code to check 'inputvalues.size()' before accessing an array item.

If you want to do something more dynamic perhaps include the API name as a CSV header row.

Name,ShippingStreet,ShippingCity,ShippingState,ShippingPostalCode,ShippingCountry
Fred Blogs,+44123456789,Fred Street,Fred City,Fred State,Fred Postalcode,Fred Country
Fred Blogs,+44123456789,Fred Street,Fred City,Fred State,Fred Postalcode,Fred Country
Fred Blogs,+44123456789,Fred Street,Fred City,Fred State,Fred Postalcode,Fred Country

Read the first row and retain the field names, then iterate over these for each subsequent row using the SObject.put method to set the field value using to the 'inputvalues' array item.

| improve this answer | |
  • AndrewFawcett Thanks for your comment....Is any other way to do adding two or more rows in excel sheet and add the csv records to Sobjects without mention the field names.By default whatever fields i load in to csv file and automatically inserted in to SObjects using apex class.... – Venkatsforce Apr 18 '13 at 6:44
  • Yes you can use the SObject.put method to dynamically assign values from the CSV columns. Your CSV header should then probably contain the API name of the field. – Andrew Fawcett Apr 18 '13 at 6:53
  • AndrewFawcett, please provide any examples like this SObject.put method because am new to salesforce...... – Venkatsforce Apr 18 '13 at 10:05
  • Please review this section of the Apex documentation on dynamic DML, salesforce.com/us/developer/docs/apexcode/Content/…. Let me know if this helps answer your original question. – Andrew Fawcett Apr 18 '13 at 10:22
  • Now i got idea from this dynamic DML...Thanks for your comment....Andrew Fawcett.......... – Venkatsforce Apr 18 '13 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.