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
Anuja Joshi 16Anuja Joshi 16 

Error on Insert records with CSV file

Hello
         I am new in SF. I have create a window using that window I want to upload csv file in "Upload Data" Custom object.The all fields have different data type like Number,Text and Currency.Problem is occured when values assigned to Number and currency field.for PF wages and ESI Wages field i took currency data Type.here pf wages value is assigned without error but for esiwages error occoured i.e.invalid decimal(at time of assign convert it into Decimal ).And Days Worked field took Number Datatype at time of assign convert it into Integer .
Invalid integer: 22
Error is in expression '{!importcsvfile}' in component <apex:commandButton> in page uploadcsvfilepage: Class.UploadCSVFileController.importcsvfile: line 50, column 1


.Below my code is given.Please help.



VF page
<apex:page sidebar="false" showHeader="false" StandardController="Account" extensions="UploadCSVFileController">
    <apex:sectionHeader title="-{!Account.name}" subtitle="{!cp.Month_Year__c}"/>
        <apex:form >    
            <apex:pageBlock >
                <apex:pageBlocksection >               
                    <apex:inputFile value="{!CsvFileBody}" fileName="{!csvfile}"/>
                </apex:pageBlocksection>
                <apex:pageBlocksection >            
                    <apex:commandButton value="OK" action="{!importcsvfile}"/>
                    <apex:commandButton value="Cancel" onclick="window.close()"/>
                </apex:pageBlocksection>
            </apex:pageBlock>
        </apex:form>    
</apex:page>



Controller is:

public class UploadCSVFileController
{
    private final Account acct;
    public id accountid{get;set;}
    public string Mon{get;set;}
    public string Yrs{get;set;}
    Public final Period__c cp{get;set;}
    Public String Monthinname;
    Public String MonthWithZero;
    public Blob CsvFileBody{get;set;}
    public String csvfile{get;set;}
    public String[] csvFileLines{get;set;}
    public list<Upload_Data__c> Uploadfilelist{get;set;}
    public UploadCSVFileController(apexpages.standardcontroller stdcontroller)
    {
        system.debug('Inside Constructor');
        csvfileLines=new String[]{};
        //csvFileLines = new String[]{};
        Uploadfilelist=new list<Upload_Data__c>();
        acct=(Account)stdcontroller.getrecord();
        accountid=acct.id;
        System.debug('The account id'+accountid);
        Mon = ApexPages.currentPage().getParameters().get('mm');
        Yrs = ApexPages.currentPage().getParameters().get('yyyy');
        System.debug('The current month is:'+Mon);
        System.debug('The current year is:'+Yrs);   
        MonthWithZero=Mon.leftpad(2,'0');
        string monthyear=MonthWithZero+'-'+yrs;
        cp=[select id,name,Month_Year__c from Period__c WHERE name=:monthyear];
        system.debug(cp);
        System.debug(cp.Month_Year__c);
    }
    public void importcsvfile()
    {
        csvfile=CsvFileBody.ToString();
        csvFileLines=csvfile.split('\n');
        
            for(Integer i=1;i<csvFileLines.size();i++)
            {
                Upload_Data__c UD=new Upload_Data__c() ;
                string[] csvRecordData = csvFileLines[i].split(',');
                UD.Employee_PF_No__c = csvRecordData[0] ;             
                UD.Employee_ESI_No__c = csvRecordData[1];
                UD.First_Name__c = csvRecordData[2];
                UD.Middle_Name__c = csvRecordData[3];   
                UD.Last_Name__c = csvRecordData[4];
                UD.PF_Wages__c=decimal.valueOf(csvRecordData[5]);
                //error is here for following 2 fields
                UD.ESI_Wages__c=decimal.valueOf(csvRecordData[6]);
                UD.Days_Worked__c=Integer.valueOf(csvRecordData[7]);                                                                             
                Uploadfilelist.add(UD);
            }
            try
            {
                insert Uploadfilelist;
            }
            catch(Exception e)
            {
                System.debug(e);
            }       
    }
    
}