You need to sign in to do that
Don't have an account?
SF Dakshendra
An error has occured reading the CSV fileList index out of bounds: 5
Here my code is
public class FileUploader
{
public string nameFile{get;set;}
public Blob contentFile{get;set;}
String[] filelines = new String[]{};
List<Account> accstoupload;
public Pagereference ReadFile()
{
try{
nameFile =blobToString( contentFile,'ISO-8859-1');
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);
}
}
catch(Exception e){
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured reading the CSV file'+e.getMessage());
ApexPages.addMessage(errormsg);
}
try{
insert accstoupload;
}
catch (Exception e)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured inserting the records'+e.getMessage());
ApexPages.addMessage(errormsg);
}
return null;
}
public List<Account> getuploadedAccounts()
{
if (accstoupload!= NULL)
if (accstoupload.size() > 0)
return accstoupload;
else
return null;
else
return null;
}
public static String blobToString(Blob input, String inCharset){
String hex = EncodingUtil.convertToHex(input);
System.assertEquals(0, hex.length() & 1);
final Integer bytesCount = hex.length() >> 1;
String[] bytes = new String[bytesCount];
for(Integer i = 0; i < bytesCount; ++i)
bytes[i] = hex.mid(i << 1, 2);
return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
}
}
public class FileUploader
{
public string nameFile{get;set;}
public Blob contentFile{get;set;}
String[] filelines = new String[]{};
List<Account> accstoupload;
public Pagereference ReadFile()
{
try{
nameFile =blobToString( contentFile,'ISO-8859-1');
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);
}
}
catch(Exception e){
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured reading the CSV file'+e.getMessage());
ApexPages.addMessage(errormsg);
}
try{
insert accstoupload;
}
catch (Exception e)
{
ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured inserting the records'+e.getMessage());
ApexPages.addMessage(errormsg);
}
return null;
}
public List<Account> getuploadedAccounts()
{
if (accstoupload!= NULL)
if (accstoupload.size() > 0)
return accstoupload;
else
return null;
else
return null;
}
public static String blobToString(Blob input, String inCharset){
String hex = EncodingUtil.convertToHex(input);
System.assertEquals(0, hex.length() & 1);
final Integer bytesCount = hex.length() >> 1;
String[] bytes = new String[bytesCount];
for(Integer i = 0; i < bytesCount; ++i)
bytes[i] = hex.mid(i << 1, 2);
return EncodingUtil.urlDecode('%' + String.join(bytes, '%'), inCharset);
}
}
Let us know if this will help you
How to use parser function in code: