+ Start a Discussion
Vijay sidaraddiVijay sidaraddi 

Issues with the insert operation while reading from csv

Hi Friends,
I  am facing issues with the insert operation while reading from csv
can you help me on that


public class ImportDataFromCSVController {
    
    public Blob csvFileBody{get;set;}
    public List<SalesInfluenceReporting__c> salesInfList{get;set;}
    public Boolean showSave{get;set;}
    public Integer salesInfCount{get;set;}
    
    public ImportDataFromCSVController(){
        showSave = false;
        salesInfCount = 0;
    }
    
    public void saveRecords() {
        ApexPages.getMessages().clear();
        
        try{
                    
                    upsert salesInfList;
                    ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.CONFIRM,'Sales Influence Report Updated Successfully');
                    ApexPages.addMessage(errorMessage);
                
            
        }catch (Exception e){
            addErrorMsg('An error has occured while saving the Sales Influence Report.');
        }
     
    }
    
    public void importCSVFile(){
        showSave = false;
        salesInfList = New List<SalesInfluenceReporting__c>();
        map<String,SalesInfWrapper> csvMap = new map<String,SalesInfWrapper>();
        Integer mmsIdColumnNo = null;
        Integer quarterColumnNo = null;
        Integer monthColumnNo = null;
        Integer clientColumnNo = null;
        Integer OGColumnNo = null;
        Integer subVertColumnNo = null;
        Integer oppNameColumnNo = null;
        Integer statusColumnNo = null;
        Integer devOffColumnNo = null;
        Integer valueColumnNo = null;
        Integer natureOfOpptyColumnNo = null;
        Integer demoDoneColumnNo = null;
        string csvAsString = '';
        list<String> csvFileLines = new list<String>();
        
        
        List<SalesInfluenceReporting__c> tempList = new List<SalesInfluenceReporting__c>();
        try{
            csvAsString = csvFileBody.toString();
            csvFileLines = csvAsString.split('\n');
        }catch (Exception e){
            addErrorMsg('An error has occured while importing data. Please make sure input csv file is correct');
            return;
        }
        
        list<String> csvHeaderData = csvFileLines[0].split(',');
        for(Integer i=0;i<csvHeaderData.size();i++){
            if(csvHeaderData[i].trim()=='MMS Id'){
                mmsIdColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Quarter'){
                quarterColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Month'){
                monthColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Client'){
                clientColumnNo = i;
            }else if(csvHeaderData[i].trim()=='OG'){
                OGColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Sub-Vertical'){
                subVertColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Opportunity Name'){
                oppNameColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Status'){
                statusColumnNo = i;
            }else if(csvHeaderData[i].trim()=='DevOps Offered'){
                devOffColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Value'){
                valueColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Nature Of Oppty'){
                natureOfOpptyColumnNo = i;
            }else if(csvHeaderData[i].trim()=='Demo Done'){
                demoDoneColumnNo = i;
            }
        }
        if(mmsIdColumnNo != null && quarterColumnNo != null && monthColumnNo != null && clientColumnNo != null && OGColumnNo != null && subVertColumnNo != null && oppNameColumnNo != null && statusColumnNo !=null && devOffColumnNo != null && valueColumnNo !=null && natureOfOpptyColumnNo != null && demoDoneColumnNo !=null ){
            
            for(Integer i=1;i<csvFileLines.size();i++){
                string[] csvRecordData = csvFileLines[i].split(',');
                SalesInfWrapper recWrapper = new SalesInfWrapper();
               
                recWrapper.mmsId = csvRecordData[mmsIdColumnNo];
                recWrapper.quarter = csvRecordData[quarterColumnNo];    
                recWrapper.month = csvRecordData[monthColumnNo];
                recWrapper.client = csvRecordData[clientColumnNo];
                recWrapper.OG = csvRecordData[OGColumnNo];
                recWrapper.subVertical = csvRecordData[subVertColumnNo];
                recWrapper.oppName = csvRecordData[oppNameColumnNo];
                recWrapper.status = csvRecordData[statusColumnNo];
                recWrapper.devOpsOff = csvRecordData[devOffColumnNo];
                recWrapper.value = csvRecordData[valueColumnNo];
                recWrapper.natureOfOpty = csvRecordData[natureOfOpptyColumnNo];
                recWrapper.demoDone = csvRecordData[demoDoneColumnNo];
                  
                csvMap.put(csvRecordData[mmsIdColumnNo],recWrapper); 
                
                //salesInfList.add(recWrapper);
                }
                
            }
            tempList = [Select MMS_Id__c, Quarter__c, Month__c, Client__c, OG__c, Opportunity_Name__c, Sub_Vertical__c,Demo_Done__c, Status__c,Value__c,Nature_of_Oppty__c, DevOps_Offered__c From SalesInfluenceReporting__c Where MMS_Id__c In : csvMap.keySet()];
            system.debug('------tempList -----'+tempList );
            system.debug('------tempList.size()-----'+tempList.size() );
            if(tempList.size()>0)
            {
                for(SalesInfluenceReporting__c currRec : tempList ){
                            currRec.MMS_Id__c = csvMap.get(currRec.MMS_Id__c).mmsId;
                            currRec.Quarter__c = csvMap.get(currRec.MMS_Id__c).quarter;
                            currRec.Month__c = csvMap.get(currRec.MMS_Id__c).month;
                            currRec.Client__c = csvMap.get(currRec.MMS_Id__c).client;
                            currRec.OG__c = csvMap.get(currRec.MMS_Id__c).OG;
                            currRec.Sub_Vertical__c = csvMap.get(currRec.MMS_Id__c).subVertical;
                            currRec.Opportunity_Name__c = csvMap.get(currRec.MMS_Id__c).oppName;
                            currRec.Status__c = csvMap.get(currRec.MMS_Id__c).status;
                            currRec.DevOps_Offered__c = csvMap.get(currRec.MMS_Id__c).devOpsOff;
                            currRec.Value__c = csvMap.get(currRec.MMS_Id__c).value;
                            currRec.Nature_of_Oppty__c = csvMap.get(currRec.MMS_Id__c).natureOfOpty;
                            currRec.Demo_Done__c = csvMap.get(currRec.MMS_Id__c).demoDone;
                            salesInfList.add(currRec);
                    }
            }
            
            else{
                addErrorMsg('No Matching Sales Influence Reports Found.');
                return;
                }
            if(salesInfList.size()>0){
                showSave = true;
                salesInfCount = salesInfList.size();
                system.debug('------countt-----'+salesInfCount );
            }
            /*else if(salesInfList.size() == 0){
                showSave = true;
                saveRecords();
            } */
            else
            {
                addErrorMsg('No Sales Influence Reports To Update. All values are up to date.');
            }
           }
            
      
    public class SalesInfWrapper{
        public string mmsId{get;set;}
        public string quarter{get;set;}
        public string month{get;set;}
        public string client{get;set;}
        public string OG{get;set;}
        public string subVertical{get;set;}
        public string oppName{get;set;}
        public string status{get;set;}
        public string devOpsOff{get;set;}
        public string value{get;set;}
        public string natureOfOpty{get;set;}
        public string demoDone{get;set;}
        
    }
    
    public void addErrorMsg(String err){
        ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,err);
        ApexPages.addMessage(errorMessage);
    }

   
}
************************vf page****************


<apex:page controller="ImportDataFromCSVController">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4"> 
                <apex:inputFile value="{!csvFileBody}"  />
                <apex:commandButton value="Upload CSV" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock id="opptySec">
            <apex:pageBlockButtons location="top">
               <apex:commandButton action="{!saveRecords}" value="Save Sales Influence Reports" /> 
            </apex:pageBlockButtons>
             <apex:outputPanel rendered="{!showSave}"><b>Number Of Opportunities : {!salesInfCount }</b></apex:outputPanel>
           <apex:pageblocktable value="{!salesInfList}" var="salesInf">
                <apex:column value="{!salesInf.MMS_Id__c}"/> 
                <apex:column value="{!salesInf.Quarter__c}"/>
                <apex:column value="{!salesInf.Month__c}"/>
                <apex:column value="{!salesInf.Client__c}"/>
                <apex:column value="{!salesInf.OG__c}"/>
                <apex:column value="{!salesInf.Sub_Vertical__c}"/>
                <apex:column value="{!salesInf.Opportunity_Name__c }"/>
                <apex:column value="{!salesInf.Status__c }"/>
                <apex:column value="{!salesInf.DevOps_Offered__c }"/>
                <apex:column value="{!salesInf.Value__c }"/>
                <apex:column value="{!salesInf.Nature_of_Oppty__c }"/>
                <apex:column value="{!salesInf.Demo_Done__c }"/>
                
                
            </apex:pageblocktable> 
        </apex:pageBlock>
    </apex:form>
</apex:page>
             
Thanks
Vijaykumar 


 
Arshadulla.ShariffArshadulla.Shariff
Hello Vijaykumar,

Please let us know the Error Message, your facing.
The one common solution you can try is 
implement for few column, then try catching the statement which is a exeption or iterate/debug each line by line code.

If your problem is solved, please mark it solved.