You need to sign in to do that
Don't have an account?

Uploading from CSV using apex getting this "Error List index out of bounds: 5
Hi All,
I am uploading a csv using apex programming to upload data to my custom object. I am getting this error while uploading csv file. Some the same is uploading correctly. Can one tell how to resolve this. I tried a lot but i couldn't resolve this.
Error:
List index out of bounds: 5
Error is in expression '{!ReadFile}' in page
Thanks,
public class NHS_Project_DataUpload_1 { public NHS_Project_DataUpload_1(ApexPages.StandardController controller) { } /*public FileUploaderAll(ApexPages.StandardController controller) { }*/ public PageReference fileAccess() { return null; } public String dateTimeValue { get; set; } public NHS_Project_DataUpload_1(){ dateTimeValue = System.Now().format('dd/MM/yyyy');//GMT } public string siteCordell_acc; public string Acc_cordell_acc; //public string opp_site_cordell_acc; public string nameFile{get;set;} public Blob contentFile{get;set;} String[] filelines = new String[]{}; List<Account> accstoupload; List<Contact> contoupload; List<Opportunity> opptoupload; List<Site__c> sitetoupload; List<String> acctNames; List<Account> newAccts = new List<Account>(); List<Account> existAccts = new List<Account>(); public Pagereference ReadFile() { nameFile=contentFile.toString(); filelines = nameFile.split('\n'); accstoupload = new List<Account>(); contoupload = new List<Contact>(); opptoupload = new List<Opportunity>(); sitetoupload = new List<Site__c>(); acctNames = new List<String>(); List<Account> existingAccts =new List<Account>(); for (Integer i=1;i<filelines.size();i++) { String[] inputvalues = new String[]{}; inputvalues = filelines[i].split(','); String Role_Description_Acct ; Role_Description_Acct = inputvalues[14]; if(( Role_Description_Acct == 'Builder') || ( Role_Description_Acct == 'Contractor') || ( Role_Description_Acct == 'Construction Manager')||(Role_Description_Acct == '')) { Account a = new Account(); a.Name = inputvalues[28]; a.BillingStreet = inputvalues[29]; a.BillingCity = inputvalues[30]; a.BillingState = inputvalues[31]; a.BillingPostalCode = inputvalues[32]; a.Phone = inputvalues[33]; a.Fax = inputvalues[34]; a.Email__c = inputvalues[35]; a.Cordell_Account_Number__c=inputvalues[13]; // a.BillingCountry = inputvalues[4]; acctNames.add(a.Name); accstoupload.add(a); } // accstoupload.add(a); } // existingAccts = [SELECT Id, Name FROM Account where name in :acctNames]; // existingAccts = [SELECT Cordell_Account_Number__c, Name FROM Account where Cordell_Account_Number__c in :acctNames]; existingAccts = [SELECT Cordell_Account_Number__c, Name FROM Account where name in :acctNames]; //create a map with names as key Map<String, Id> acctNamesIdMap = new Map<String, Id>(); //Creating Map for with names as key for exising account to create Opportunity // load the map - this will help you find out if an account name exists already for (Account acct : existingAccts) { acctNamesIdMap.put(acct.Name, acct.Id); //acctNamesIdMap.put(acct.Name, acct.Cordell_Account_Number__c);//checking existing account using cordell account no //existAccts=[SELECT Cordell_Account_Number__c FROM Account where name in :acctNames]; System.debug('******Voltum******'); ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Account ' + acct.Name + ' already exists,Duplicate Account Name not allowed'); ApexPages.addMessage(errormsg); } for (Account acct : accstoupload) { //if account name does not exist in map, add it to list of new accounts // if (!acctNamesIdMap.containsKey(acct.Cordell_Account_Number__c)) if (!acctNamesIdMap.containsKey(acct.Name)) { newAccts.add(acct); ApexPages.Message successmsg = new ApexPages.Message(ApexPages.severity.confirm,'New Account ' + acct.Name + ' is created,related Contact, Opportunity and Site record also created'); ApexPages.addMessage(successmsg); } } try { insert newAccts; /* ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.info,'AccountName with Account Id are the new Account created.Related Contact and Opportunity also created'); ApexPages.Message msg1 = new ApexPages.Message(ApexPages.severity.info,'AccountName without Account Id are the existing Accounts'); ApexPages.addMessage(msg); ApexPages.addMessage(msg1);*/ } catch (Exception e) { // update existAccts; ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'Account Name already exist, change Account Name and try again'); ApexPages.addMessage(errormsg); } //Query Inserted Records for Account and Builts a Map (mapAccInserted) Set<String> setAccCordellId = new Set<String>(); for (Account acct : accstoupload) { if(!setAccCordellId.contains(acct.Cordell_Account_Number__c)) { setAccCordellId.add(acct.Cordell_Account_Number__c); } } Map<String, Account> mapAccInserted = new Map<String, Account>(); Map<String, Account> mapAccInserted1 = new Map<String, Account>(); List<Account> lstAccInserted = new List<Account>([SELECT Id, Name, Cordell_Account_Number__c FROM Account WHERE Cordell_Account_Number__c in :setAccCordellId]); for(Account a: lstAccInserted) { mapAccInserted.put(a.Cordell_Account_Number__c, a); //mapAccInserted1.put(a.Id, a); } //accstoupload = [SELECT Id, Name FROM Account WHERE Cordell_Account_Number__c in :setAccCordellId]; /* for(Account a: accstoupload) { mapAccInserted1.put(a.Id, a); }*/ system.debug('>>>mapAccInserted' + mapAccInserted); //return null; for (Integer i=1;i<filelines.size();i++) { String[] inputconvalues = new String[]{}; inputconvalues = filelines[i].split(','); Contact con = new Contact(); /*****************************************************/ String Cordell_AccNo_Con; //String Acc_Id; Cordell_AccNo_Con = inputconvalues [13]; //Contact con = new Contact(); /************************************ getting Account for contact ***********************************/ /*for(account a: accstoupload) { Account Acc_Id = mapAccInserted1.get(a.Id); // Account Acc_cordell_acc_con = mapAccInserted.get(Cordell_AccNo_Con); // Account Acc_cordell_acc_con = mapAccInserted.get(Acc_Id); system.debug('>>>Map Acc cordell_insert:' +Acc_cordell_acc); if( Acc_Id !=null) //if(!mapAccInserted.containsKey( Acc_cordell_acc_con)) // con.AccountId = Acc_cordell_acc_con.Id; con.AccountId = Acc_Id.Id; } if(con.AccountId !=null) { //opp.AccountId = Acc_cordell_acc.Account__c; con.Lastname = inputconvalues[36]; con.Email = inputconvalues[37]; con.Phone = inputconvalues[38]; contoupload.add(con); }*/ /*************************************************************/ /*for(account a: accstoupload) { con.AccountId = accstoupload.id; //con.AccountId = accstoupload[i-1].id; //********con.AccountId = accstoupload[i-1].Cordell_Account_Number__c; } if(con.AccountId!= null) { con.Lastname = inputconvalues[36]; con.Email = inputconvalues[37]; con.Phone = inputconvalues[38]; contoupload.add(con); }*/ for(Account a: accstoupload) { //con.AccountId = accstoupload.id; con.AccountId =accstoupload[i-1].id; //********con.AccountId = accstoupload[i-1].Cordell_Account_Number__c; } if(con.AccountId!= null) { con.Lastname = inputconvalues[36]; con.Email = inputconvalues[37]; con.Phone = inputconvalues[38]; contoupload.add(con); } } try { insert contoupload; } catch (Exception e1) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.INFO,'Account Name associate with this Contact already exist '); ApexPages.addMessage(errormsg); } // return null; for (Integer i=1;i<filelines.size();i++) { String[] inputsitevalues = new String[]{}; inputsitevalues = filelines[i].split(','); String Role_Description_site; //Site__c site= new Site__c(); //to get the acc_id for site /*for(account a: accstoupload) { //site.Site_Acc__c = accstoupload[i-1].id; site.Cordell_Account_No__c= accstoupload[i-1].Cordell_Account_Number__c; } */ //site.CompanyName__c = inputsitevalues[0]; // no account exist create new site //if(site.Cordell_Account_No__c!= null) //if(site.Cordell_Account_No__c== null) //{ //Project import page //************* new site creating if cordell numberis not in account ******/ Role_Description_site= inputsitevalues[14]; if((Role_Description_site == 'Builder') || (Role_Description_site == 'Contractor') || (Role_Description_site == 'Construction Manager')|| (Role_Description_site == '')) { Site__c site= new Site__c(); site.Cordell_Account_No__c = inputsitevalues[13]; system.debug('>>>Map Acc INserted:' + mapAccInserted.get(site.Cordell_Account_No__c)); /*************************/ Account siteCordell_acc = mapAccInserted.get(site.Cordell_Account_No__c); if(siteCordell_acc!= null) { site.Account__c = siteCordell_acc.Id; } site.Project_ID__c = inputsitevalues[0]; site.Project_Title__c = inputsitevalues[1]; site.Site_Address__c = inputsitevalues[2]; site.Project_City__c = inputsitevalues[3]; site.State__c= inputsitevalues[4]; site.Post_code__c= inputsitevalues[5]; site.Council__c = inputsitevalues[6]; site.Type__c = inputsitevalues[7]; site.Project_First_Reported__c =date.today();//**Date field** site.Estimated_Value__c =decimal.valueOf(inputsitevalues[9]); site.Project_Details__c = inputsitevalues[10]; site.Project_Additional_Details__c= inputsitevalues[11]; site.Development_Type__c = inputsitevalues[12]; if(site.Development_Type__c=='New') site.Category_s__c = 'Commercial - New'; else site.Category_s__c = 'Commercial - Add & Alter'; site.Role_Description__c = inputsitevalues[14]; site.Tender_Type__c = inputsitevalues[15]; site.Floor_Area__c = integer.valueOf(inputsitevalues[16]); site.Floors__c = integer.valueOf(inputsitevalues[17]); site.Ownership__c = inputsitevalues[18]; //site.Tender_Due_Date__c =date.today();//**date field** [19] site.Tender_Due_Date__c = date.parse(inputsitevalues[19]);//**date field[19] //site.Commence_Date__c =date.today();//**date field**[20] site.Commence_Date__c= date.parse(inputsitevalues[20]);//**date field[20] // site.Completion_Date__c=date.today();//**date field**[21] site.Completion_Date__c= date.parse(inputsitevalues[21]);//**date field[21] //site.Last_Updated__c = date.today();//**date field**[22] site.Last_Updated__c = date.parse(inputsitevalues[22]);//**date field[22] //site.Status__c = inputsitevalues[23]; site.Note__c = inputsitevalues[24]; site.Project_Stage__c = inputsitevalues[25]; site.Site_Area__c =integer.valueOf(inputsitevalues[26]); site.Units__c = integer.valueOf(inputsitevalues[27]); //site.Account__c=inputsitevalues[28];//Main Contractor site.Cordell_Account_No__c=inputsitevalues[13]; site.Company_Name__c = inputsitevalues[28]; site.Company_Address__c = inputsitevalues[29]; site.Company_City__c = inputsitevalues[30]; site.Company_State__c= inputsitevalues[31]; site.Company_Postal_Code__c = inputsitevalues[32]; site.Company_Phone__c = inputsitevalues[33]; site.Company_Fax__c = inputsitevalues[34]; site.Company_Email__c= inputsitevalues[35]; site.Contact_Name__c = inputsitevalues[36]; site.Contact_Email__c = inputsitevalues[37]; site.Contact_Mobile__c = inputsitevalues[38]; site.Name =inputsitevalues[1] ;// Project title is site name site.Source_s__c = 'Cordell'; /***************************************************************************/ // site.Company_ID__c = integer.valueOf(inputsitevalues[13]); // site.Cordell_Account_No__c = inputsitevalues[13]; //system.debug('>>>Map Acc INserted:' + mapAccInserted.get(site.Cordell_Account_No__c)); /*siteCordell_acc = mapAccInserted.get(site.Cordell_Account_No__c).Id; if(siteCordell_acc!= null) { site.Account__c = siteCordell_acc; }*/ //site.Role_Description__c = inputsitevalues[14]; /*************** end of new site reating if cordell account number in account **************/ sitetoupload.add(site); } } try { insert sitetoupload; ApexPages.Message sitecreatedmsg = new ApexPages.Message(ApexPages.severity.confirm,'New Site Lead created'); ApexPages.addMessage(sitecreatedmsg); } catch (Exception e3) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later'); ApexPages.addMessage(errormsg); } //Query Inserted Records for Site and Builts a Map (mapSiteInserted) Set<String> setSiteCordellId = new Set<String>(); for (Site__c site : sitetoupload) { if(!setSiteCordellId.contains(site.Cordell_Account_No__c)) { setSiteCordellId.add(site.Cordell_Account_No__c); } } Map<String, Site__c> mapSiteInserted = new Map<String, Site__c>(); List<Site__c> lstSiteInserted = new List<Site__c>([SELECT Id, Name, Cordell_Account_No__c, Site_Ref_No__c, Account__c,Project_ID__c, Site_Address__c FROM Site__c WHERE Cordell_Account_No__c in :setSiteCordellId]); for(Site__c s: lstSiteInserted) { mapSiteInserted.put(s.Cordell_Account_No__c+s.Project_ID__c+s.Site_Address__c, s); } for (Integer i=1;i<filelines.size();i++) { String[] inputvalues = new String[]{}; inputvalues = filelines[i].split(','); Opportunity opp = new Opportunity(); /*for(account a: accstoupload) { opp.AccountId = accstoupload[i-1].id; //opp.AccountId = accstoupload[i-1].Cordell_Account_Number__c; //opp.AccountId = a.Id; } if(opp.AccountId!= null) {*/ opp.Name = inputvalues[0]+'-'+ inputvalues[1]; //opp.CloseDate =date.parse(inputvalues[21]); opp.StageName = 'No Contact Made'; opp.Cordell_AccNo_Opp__c = inputvalues[13]; opp.CloseDate = date.today()+7; opp.ProjectID_PermitNoOfSite__c=inputvalues[0]; opp.Project_SiteAddress__c=inputvalues[2]; /*siteCordell_acc = mapAccInserted.get(site.Cordell_Account_No__c).Id; if(siteCordell_acc!= null) { // site.Account__c = siteCordell_acc.Id; site.Account__c = siteCordell_acc; } */ /*********************************/ /*Acc_cordell_acc = mapSiteInserted.get(inputvalues[13]).Id; system.debug('>>>Map Acc cordell_insert:' +Acc_cordell_acc); if(Acc_cordell_acc !=null) opp.AccountId = Acc_cordell_acc; opp_site_cordell_acc =mapSiteInserted.get(inputvalues[13]).Id; if(opp_site_cordell_acc != null) opp.Site_Ref_No__c = opp_site_cordell_acc; */ //Site__c Acc_cordell_acc = mapSiteInserted.get(inputvalues[13]); Account Acc_cordell_acc = mapAccInserted.get(opp.Cordell_AccNo_Opp__c); system.debug('>>>Map Acc cordell_insert:' +Acc_cordell_acc); if(Acc_cordell_acc !=null) //opp.AccountId = Acc_cordell_acc.Account__c; opp.AccountId = Acc_cordell_acc.Id; // Site__c opp_site_cordell_acc =mapSiteInserted.get(inputvalues[13]+inputvalues[0]+inputvalues[2]); Site__c opp_site_cordell_acc =mapSiteInserted.get(opp.Cordell_AccNo_Opp__c+opp.ProjectID_PermitNoOfSite__c+opp.Project_SiteAddress__c); if(opp_site_cordell_acc != null) opp.Site_Ref_No__c = opp_site_cordell_acc.Site_Ref_No__c; opptoupload.add(opp); //} } try { insert opptoupload; } catch (Exception e2) { 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; } public List<Contact> getuploadedContact() { if (contoupload!= NULL) if (contoupload.size() > 0) return contoupload; else return null; else return null; } public List<Opportunity> getuploadedOpportunity() { if (opptoupload!= NULL) if (opptoupload.size() > 0) return opptoupload; else return null; else return null; } public List<Site__c> getuploadedSite() { if (sitetoupload!= NULL) if (sitetoupload.size() > 0) return sitetoupload; else return null; else return null; } /* public pageReference fileAccess(){ Document lstDoc = [select id,name,Body from Document where name = 'test']; System.Debug('DOC NAME :: '+lstDoc.name); System.Debug('DOCBODY :: '+lstDoc.Body); return null; } */ public static testMethod void testReadFile1() { Document lstDoc = [select id,name,Body from Document where name = 'test']; // System.Debug('DOC NAME :: '+lstDoc.name); //System.Debug('DOCBODY :: '+lstDoc.Body); //FileUploader file=new FileUploader(); //FileUploaderAll file=new FileUploaderAll(); NHS_Project_DataUpload file=new NHS_Project_DataUpload(); Blob content= lstDoc.Body; file.nameFile=content.toString(); } public static testMethod void testReadFile2() { } }
Regards,
Lavanya.