[SalesForce] Upload csv file data to SObjects

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

Best Answer

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.

Related Topic