function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
venkateshyadav1243venkateshyadav1243 

bulk upload csv file using batch apex

Hi

Am uploading csv file in visualforce page and uploading through bacth apex,its working fine for insert and upadte below is my code.
I have to show error messgae before inserting/update into the system.
Example i have 1 percentage field(Gss percentage) if any record have less than 100 % i need to cancel the batch process and i have to display error message in visualforce page i.e (Some X record have less than 100% please correct and enter).

When i try to add visulforce messgae in batch apex its showing me error how can i display this error in visualforce page
please can any one give me some idea.


global class CustomIterableBatchForAccount implements Database.Batchable<String>,Database.Stateful{
   
    public String CSVFile;
     set<string> strset=new set<string>();
    Map<string,id> offmap=new map<string,id>();
    public  List<GSS_Split__c> gsstoupload;
    public  List<GSS_Split__c>  gsstoupdate;
        public  List<GSS_Split__c> gsssize;
    public string errormsgs;
    //Constructor to hold the uploaded CSV file
   
    global CustomIterableBatchForAccount(String CSVFile){
        this.CSVFile= CSVFile;
    }
    public void data()
    {
   
       
    }
    //Start Method
    global Iterable<String> start(Database.BatchableContext bc){
        return new CustomIterable(CSVFile);
    }
    //execute method
    global void execute(Database.BatchableContext bc,List<String> scope){
     gsstoupload = new List<GSS_Split__c>();
    gsstoupdate = new List<GSS_Split__c>();
    gsstoupload.clear();
    gsstoupdate.clear();
   for(GSS_Split__c gss:[select name,Request_Number__c,Account_Index__c,Period_End_text__c,Period_Start_text__c,HQ__c,GRS_REPTYPE__c,GRS_PLATFORM__c,Territory__c,Region__c,WGSS_Perc__c,GSS_Perc__c,PeriodStart__c,Period_End__c,AX_login__c,Primary_AX__c from GSS_Split__c])
        {       
       // gsssize.add(gss.name);
          strset.add(gss.Request_Number__c);
          offmap.put(gss.Request_Number__c,gss.id);
          system.debug('All GSS ACCOUNT OBJECT RECORDS'+gss);
          //system.debug('All GSS ACCOUNT OBJECT RECORDS SIZE IN CURRENT SYSTEM'+gsssize.size());
        }
        List<String> inputvalues;
        try{       
            system.debug('**scope size**'+scope.size());

         
            for (Integer i=0;i<scope.size();i++)
            {
                inputvalues = new List<String>();
                //split the rows
                inputvalues = scope[i].split(',');
                if(strset.contains(inputvalues[0]))
            {
                GSS_Split__c a = new GSS_Split__c(id=offmap.get(inputvalues[0]));
               
                //split the rows
               // inputvalues = scope[i].split(',');


            a.Request_Number__c=inputvalues[0];
            a.Account_Index__c= inputvalues[1];
            a.HQ__c= inputvalues[2];      
            a.GRS_REPTYPE__c= inputvalues[3];
            a.GRS_PLATFORM__c= inputvalues[4];
            a.Territory__c= inputvalues[5];
            a.Region__c= inputvalues[6];
            a.GSS_Perc__c= integer.valueof(inputvalues[7]);
            a.WGSS_Perc__c=integer.valueof(inputvalues[8]);
            a.Period_Start_text__c=inputvalues[9];
            a.Period_End_text__c=inputvalues[10];
            a.AX_login__c= inputvalues[11];
            a.Primary_AX__c= inputvalues[12];
            gsstoupdate.add(a);
            }
             else{  
            GSS_Split__c a = new GSS_Split__c(id=offmap.get(inputvalues[4]));
            system.debug('gsss recordsssssss'+a);
            a.Request_Number__c=inputvalues[0];
            a.Account_Index__c= inputvalues[1];
            a.HQ__c= inputvalues[2];      
            a.GRS_REPTYPE__c= inputvalues[3];
            a.GRS_PLATFORM__c= inputvalues[4];
            a.Territory__c= inputvalues[5];
            a.Region__c= inputvalues[6];
            a.GSS_Perc__c= integer.valueof(inputvalues[7]);
            a.WGSS_Perc__c=integer.valueof(inputvalues[8]);
            a.Period_Start_text__c=inputvalues[9];
            a.Period_End_text__c=inputvalues[10];
            a.AX_login__c= inputvalues[11];
            a.Primary_AX__c= inputvalues[12];
            gsstoupload.add(a);
           
            }
            }
           
            try{
        if(gsstoupdate.size()>0)
        {
        system.debug('######## gsstoupdate'+gsstoupdate);
update gsstoupdate;

        }
        if(gsstoupload.size()>0)
        {
         system.debug('######## gsstoupload'+gsstoupload);
Database.insert(gsstoupload,false);

        }
             }
        catch (Exception e)
        {
       
         system.debug('Exception--->'+e+'%%%'+e.getmessage());
       
        }   
            system.debug('@@@@@@@@@@@@@@@@@@@size'+gsstoupload.size());
          
        }catch(Exception e){
            system.debug('Exception--->'+e+'%%%'+e.getmessage());
        }
       
    }
   
    global void finish(Database.BatchableContext bc)
    {
    }
}
Regards,
Venkatesh.