You need to sign in to do that
Don't have an account?
krishna jammigumpula
While uploading csv file i want check it if csv is empty i want sent error
scenario 1) whie uploading csv i want to check is it has data or not if data is not having i want throw error
scenario 2) whie uploading csv i want to check is it has data if any column not having i want throw error
For this i have written below code
Apex class
========
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public void importCSVFile(){
List<account> acclist = New List<Account>();
string csvAsString = csvFileBody.toString();
system.debug(csvAsString);
String[] csvFileLines = new String[]{};
if(csvFileLines.size()==1 ){
csvFileLines = csvAsString.split('\n');
system.debug(csvFileLines);
for(Integer i=1;i<csvFileLines.size();i++){
system.debug(i);
Account accObj = new Account() ;
string[] csvRecordData = csvFileLines[i].split(',');
system.debug(csvRecordData);
accObj.name = csvRecordData[0] ;
accObj.accountnumber = csvRecordData[1];
accObj.Type = csvRecordData[2];
accObj.AccountSource = csvRecordData[3];
accObj.Industry = csvRecordData[4];
if(accObj.name !=null && accObj.accountnumber !=null && accObj.Type !=null && accObj.AccountSource!=null && accObj.Industry!=null ){
acclist.add(accObj);
}
try{
insert acclist;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Quote Source/s inserted successfully'));
} catch (Exception e){
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importing data Please make sure input csv file is correct :'+e.getMessage());
ApexPages.addMessage(errorMessage);
}
}
}
}
}
Visual force page
=============
<apex:page controller="importDataFromCSVController">
<apex:form >
<apex:pagemessages />
<apex:pageBlock >
<apex:pageBlockSection columns="4">
<apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>
<apex:commandButton value="Import Account" action="{!importCSVFile}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
any help will be apriciate
Thanks in advance
scenario 2) whie uploading csv i want to check is it has data if any column not having i want throw error
For this i have written below code
Apex class
========
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public void importCSVFile(){
List<account> acclist = New List<Account>();
string csvAsString = csvFileBody.toString();
system.debug(csvAsString);
String[] csvFileLines = new String[]{};
if(csvFileLines.size()==1 ){
csvFileLines = csvAsString.split('\n');
system.debug(csvFileLines);
for(Integer i=1;i<csvFileLines.size();i++){
system.debug(i);
Account accObj = new Account() ;
string[] csvRecordData = csvFileLines[i].split(',');
system.debug(csvRecordData);
accObj.name = csvRecordData[0] ;
accObj.accountnumber = csvRecordData[1];
accObj.Type = csvRecordData[2];
accObj.AccountSource = csvRecordData[3];
accObj.Industry = csvRecordData[4];
if(accObj.name !=null && accObj.accountnumber !=null && accObj.Type !=null && accObj.AccountSource!=null && accObj.Industry!=null ){
acclist.add(accObj);
}
try{
insert acclist;
ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM,'Quote Source/s inserted successfully'));
} catch (Exception e){
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importing data Please make sure input csv file is correct :'+e.getMessage());
ApexPages.addMessage(errorMessage);
}
}
}
}
}
Visual force page
=============
<apex:page controller="importDataFromCSVController">
<apex:form >
<apex:pagemessages />
<apex:pageBlock >
<apex:pageBlockSection columns="4">
<apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>
<apex:commandButton value="Import Account" action="{!importCSVFile}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
any help will be apriciate
Thanks in advance