+ Start a Discussion
Amita TatarAmita Tatar 

limit of records while importing data from csv using vf page

Hi,

I have written following apex code to import data from csv using vf page.
but it is importing only 200 records. If i insert more than 200 , it ignores first 200 records.
Can you please help me here
Apex Code:

/* File Name : importDataFromCSVControllerMandE.apxc
 * Description : This apex controller reads imported CSV file and creates Guests and Delegates for contact.
 *              It also creates new Contact record if that particular Contact is not present in the system.
 * @@Author : Aress Software and Education Technologies Pvt. Ltd.
 * Created Date : 27/07/2017
 * Last Modified Date : 28/08/2017
*/

public class importDataFromCSVControllerMandE {
	
    //constructor
    public importDataFromCSVControllerMandE(ApexPages.StandardController controller) {
    }
    
    public string OppId;
    public string AcctId;
    public Blob csvFileBody{get;set;}
    public String csvAsString{get;set;}
    public String[] csvFileLines{get;set;}
    public List<Guests_Delegate__c> gdlist{get;set;}
    Set<String> oIds = new Set<String>();
    Set<String> emailIds = new Set<String>();
    
    Map<String,Id> accounts = new Map<String,Id>();
    // Map<String,Id> accountsFromCSV = new Map<String,Id>();
    List<Contact> newContactList = new List<Contact>();
    
    Set<String> CSVNames = new Set<String>();
    Map<String,Id> ContMap = new Map<String,Id>();
    
    //constructor
    public importDataFromCSVControllerMandE(){
        csvFileLines = new String[]{};
        gdlist= New List<Guests_Delegate__c>();
    }
    /*
* Method Name - importCSVFile()
* Description - This method invokes when "Import Guests & Delegates" button is clicked. 
*				It reads the imported CSV file and creates Guests and Delegates accordingly.
*				If the system come across new Email in the CSV template, then it is able to create new Contact for that record.
*				The method can able to handle the exception, if any type of error occurs.
*/
    public void importCSVFile(){
        try{
            OppId = apexpages.currentpage().getparameters().get('oppId');
            system.debug('Opportunity Id - > '+OppId);
            AcctId = apexpages.currentpage().getparameters().get('acctId');
            system.debug('Account Id - > '+AcctId);
            
            // Read CSV file body and store it in variable
            csvAsString = csvFileBody.toString();
            system.debug(' csvAsString******************* '+ csvAsString);
            
            List<Guests_Delegate__c> PrevGuestList = [SELECT Id FROM Guests_Delegate__c WHERE Booking__c =: OppId];
            if(csvAsString!='' || csvAsString != NULL){
                if(PrevGuestList.size() > 0){
                	delete PrevGuestList;   //Delete the previous Guest and Delegates and then add latest uploaded ones.
                }
            }
            
            // Split CSV String to lines
            csvFileLines = csvAsString.split('\n');
            system.debug(' csvFileLines******************* '+ csvFileLines.size());
            
            //************************** To check for contact ******************************
            //for(Integer i=1;i< csvFileLines.size() ; i++){
            //    String[] csvRecordDataCheck = csvFileLines[i].split(',');
            //    emailIds.add(csvRecordDataCheck[5]);
            //    system.debug('emailIds-------'+emailIds);
            //}
            
            // Mapping by first name and last name
            for(Integer i=1 ; i< csvFileLines.size() ; i++){
                system.debug('In first name and last name mapping loop');
                String[] csvRecordData = csvFileLines[i].split(',');
                system.debug('csvRecordData -> '+csvRecordData);
                string names = csvRecordData[1]+ ' ' +csvRecordData[2];
                CSVNames.add(names);
            }
            system.debug('CSVNames -> '+CSVNames);
            
            // getting account Id from the records who are already present in contact
            List<Contact> cList = [SELECT id, name, firstname, lastname, email, AccountId FROM contact WHERE name in : CSVNames];
            system.debug('cList********'+cList);
            
            //Map<String, Id> contactMap = new Map<String, Id>();
            //for(Contact c : cList){
            //contactMap.put(c.Email, c.Id);
            //}
            
            ContMap = new Map<String,Id>();
            for(Contact c : cList){
                ContMap.put(c.Name, c.id);
            }
            system.debug('ContMap -> '+ContMap);
            
             Id gDRecTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('Guest & Delegate Contact').getRecordTypeId();
            system.debug('G and D Record Type -> '+gDRecTypeId);
            
            for(Integer i=1; i < csvFileLines.size(); i++){
                String[] csvRecordDatacon = csvFileLines[i].split(',');
                string names = csvRecordDatacon[1]+ ' ' +csvRecordDatacon[2];
                if(!ContMap.containsKey(names)){
                    Contact con = new Contact();
                    //record type here
                    //con.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('VIP Client Contact').getRecordTypeId();
                    //system.debug(' con.RecordTypeId'+ con.RecordTypeId);
                    //con.AccountId = csvRecordDatacon[3];
                    con.AccountId = AcctId;
                    con.Guest__c = TRUE;
                    con.MobilePhone = csvRecordDatacon[4];
                    con.Email = csvRecordDatacon[5];
                    con.Salutation = csvRecordDatacon[0];
                    con.FirstName = csvRecordDatacon[1];
                    con.LastName = csvRecordDatacon[2];
                    con.Title = csvRecordDatacon[3];
                    con.RecordTypeId = gDRecTypeId;
                    newContactList.add(con);
                    system.debug('Contact added'+newContactList);
                }
            }
            insert newContactList;
            system.debug('newContactList after ->'+newContactList);
            
            //All Contact List with new Contacts from CSV
            cList = [select id,name,firstname, lastname, email, AccountId from contact where name in : CSVNames];
            system.debug('cList********'+cList);
            
            ContMap = new Map<String, Id>();
            for(Contact c : cList){
                ContMap.put(c.Name, c.Id);
            }
            system.debug('*******ContMap*****'+ContMap);
            
            // Iterate CSV file lines and retrieve one column at a time.
            system.debug(' csvFileLines.size()****************'+ csvFileLines.size());
            for(Integer i=1; i < csvFileLines.size(); i++){
               
                
                Guests_Delegate__c gdObj = new Guests_Delegate__c();
                String[] csvRecordData = csvFileLines[i].split(',');
                system.debug('csvRecordData765-------------------'+csvRecordData);
                
                gdObj.Work_Phone__c = csvRecordData[4];
                system.debug(' gdObj.Work_Phone__c*************'+ gdObj.Work_Phone__c);
              
                //gdObj.Booking__c = csvRecordData[1];
                //system.debug('gdObj.Booking__c==========='+ gdObj.Booking__c );
                //Updated and fetched from URL
                gdObj.Booking__c = OppId;
                system.debug('gdObj.Booking__c==========='+ gdObj.Booking__c );
                
                //gdObj.Account__c =  csvRecordData[3]; 
                //system.debug(' gdObj.Account__c*************'+ gdObj.Account__c);
                //Updated and fetched from URL
                gdObj.Account__c =  AcctId;
                system.debug(' gdObj.Account__c*************'+ gdObj.Account__c);
                
                gdObj.Email__c = csvRecordData[5];
                
                system.debug('csvRecordData[6] -> '+csvRecordData[6]);
                
                //Accepting data in DD-MM-YYYY from csv file and changing it to YYYY-MM-DD as salesforce accepts this date format.
                string a  = csvRecordData[6];
                String a_date = a.substring(0,2);
                system.debug('String a_date----------'+a_date);
                String a_month = a.substring(3,5);
                system.debug('String a_month----------'+a_month);
                string a_year = a.substring(6,10);
                system.debug('String a_year----------'+a_year);
                string arrival_date = a_year+'-'+a_month+'-'+a_date; //modifying the date as per Salesforce's convensions.
                system.debug('String arrival_date----------'+arrival_date);
                
                gdObj.Arrival_Date__c = date.valueOf(arrival_date);
                system.debug('gdObj.Arrival_Date__c*************'+ gdObj.Arrival_Date__c);
                
                //Accepting data in DD-MM-YYYY from csv file and changing it to YYYY-MM-DD as salesforce accepts this date format.
                //string d  = csvRecordData[7];
                //String d_date = a.substring(0,2);
                //String d_month = a.substring(3,5);
                //string d_year = a.substring(6,10);
                //string departure_date = a_year+'-'+a_month+'-'+a_date;
                
              //  gdObj.Departure_Date__c= date.valueOf(departure_date);
               // system.debug('gdObj.Departure_Date__c*************'+ gdObj.Departure_Date__c);
               decimal nights = decimal.ValueOf(csvRecordData[7]); 
               system.debug('nights--------'+nights);
               gdObj.No_of_Nights__c= nights;
               system.debug('gdObj.No_of_Nights__c*************'+ gdObj.No_of_Nights__c);
                
                //adding Departure date
                gdObj.Departure_Date__c = gdObj.Arrival_Date__c.addDays(Integer.valueOf(gdObj.No_of_Nights__c));
                
                Decimal rooms = Decimal.valueOf(csvRecordData[8]);
                gdObj.No_of_Rooms__c = rooms;
                system.debug('gdObj.No_of_Rooms__c*************'+ gdObj.No_of_Rooms__c);
                
                gdObj.Room_Type__c= csvRecordData[9];
                system.debug('gdObj.Room_Type__c==========='+  gdObj.Room_Type__c);
                
                gdObj.Occupancy__c= csvRecordData[10]; 
                system.debug(' gdObj.Occupancy__c*************'+ gdObj.Occupancy__c); 
                
                gdObj.Sharing_With__c = csvRecordData[11];
                gdObj.Meal_Plan__c =  csvRecordData[12];
                system.debug(' gdObj.Meal_Plan__c*************'+ gdObj.Meal_Plan__c); 
                
               // gdObj.Contact__c = contactMap.get(csvRecordData[11]);
                
                gdObj.Allowancesv2__c= csvRecordData[13];
                system.debug('gdObj.Allowances__c==========='+gdObj.Allowancesv2__c);
				
                gdObj.Parking_Required__c = boolean.valueOf(csvRecordData[14]);
                system.debug('gdObj.Parking_Required__c*************'+ gdObj.Parking_Required__c);
                
                gdObj.Number_Plate__c = csvRecordData[15];
                system.debug('gdObj.Number_Plate__c*************'+ gdObj.Number_Plate__c);
                
                gdObj.Notes__c = csvRecordData[16];
                system.debug('gdObj.Notes__c*************'+ gdObj.Notes__c);
                
                gdObj.Special_Food_Requirements__c = csvRecordData[18];
                system.debug('gdObj.Special_Food_Requirements__c*************'+ gdObj.Special_Food_Requirements__c);
                
                Decimal rate = Decimal.valueOf(csvRecordData[17]);
                gdObj.Rate__c = rate;
                system.debug('gdObj.Rate__c*************'+ gdObj.Rate__c);
                
                gdObj.Contact_Type__c = csvRecordData[19];
                system.debug('gdObj.Contact_Type__c*************'+ gdObj.Contact_Type__c);
                
                gdObj.Shift__c = csvRecordData[20];
                system.debug('gdObj.Shift__c*************'+ gdObj.Shift__c);
                
                gdObj.Commissionable__c = csvRecordData[21];
                system.debug('gdObj.Commissionable__c*************'+ gdObj.Commissionable__c);
                
                gdObj.Non_Commissionable_Reasons__c = csvRecordData[22];
                system.debug('gdObj.Non_Commissionable_Reasons__c*************'+ gdObj.Non_Commissionable_Reasons__c);
                
                gdObj.VAT_on_Commission__c = csvRecordData[24];
                system.debug('gdObj.VAT_on_Commission__c*************'+ gdObj.VAT_on_Commission__c);
                
                Decimal commPerAct = Decimal.valueOf(csvRecordData[23]);
                gdObj.Commission_Percentage_Actual__c = commPerAct;
                system.debug('gdObj.Commission_Percentage_Actual__c*************'+ gdObj.Commission_Percentage_Actual__c);
                
                string fullname = csvRecordData[1] + ' ' + csvRecordData[2];
                gdObj.Contact__c = ContMap.get(fullname);
                
                gdlist.add(gdObj);
            }
            
            if(gdlist.size()>0){
                insert gdlist;
                system.debug('GD list -> '+gdlist);
                system.debug('Size -----------------'+ gdlist.size());
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Confirm,'Data Imported Successfully'));
            }
            else{
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Import Valid Data'));
            }
        }
        catch(Exception e){
            //ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importing data into Salesforce. Please make sure input csv file is correct');
            //ApexPages.addMessage(errorMessage);
            system.debug('Error - >>>>'+e); 
        }
    }
}

 
Waqar Hussain SFWaqar Hussain SF
Hi Amita,

I looked into your code and did not found any thing which is ignoring first 200 records.