This is vf page<apex:page id="pageId" controller="importDataFromCSVController" sidebar="false" showHeader="false">
<apex:pageform id="pageId"id="formId">
controller="importDataFromCSVController" sidebar="false" showHeader="false"> <script>
<apex:formfunction id="formId">HideMsg(istrue)
{
<apex:pagemessages /> var checkval = document.getElementById("msg")
<apex:pageBlockif(istrue.checked)
id="pageblockId">
{
var checkval = document.getElementById("msg").style.display='block';
}
else
{
var checkval = document.getElementById("msg").style.display='none';
}
}
</script>
<apex:pageBlockSectionpagemessages Id="pageblocksectionId">/>
<apex:pageBlock id="pageblockId">
<apex:pageBlockSection Id="pageblocksectionId">
<apex:inputFile value="{!csvFileBody}" filename="{!csvAsString}"/>
<apex:commandButton value="Import Project" action="{!importCSVFile}"/>
</apex:pageBlockSection>
<apex:pageBlock >
<apex:pageBlockpageBlockSection title="Project List" columns="1" >
<apex:pageBlockSectionpageBlockTable title="Projectvalue="{!Project2}" List"var="a" columns="1"border="1">
<apex:column value="{!a.Name}"/>
<apex:column >
<!--<apex:pageBlockTable value="{!Project2a.Project_Tasks__r}" var="a" border="1">var="c">
<apex:column value="{!ac.Name}"/>
<apex:column </apex:pageBlockTable>-->
<div>
<apex:inputCheckbox id="checkbox" onchange="HideMsg(this); "/>
</apex:column>div>
<apex<div class="showp" style="display:column> none" id="msg">
<apex:pageBlockTablerepeat value="{!a.Project_Tasks__r}" var="c">
<apex:columnoutputText value="{!c.Name}"/>">
</apex:pageBlockTable>outputText>
</apex:column>repeat>
<apex:column value="{!a.Developer_Name__c}"</>div>
</apex:column>
<apex:column value="{!a.Client_Name__cDeveloper_Name__c}"/>
<apex:column value="{!a.Date__cClient_Name__c}"/>
<apex:column value="{!a.Start_Date__cDate__c}"/>
<apex:column value="{!a.End_Date__cStart_Date__c}"/>
<apex:column value="{!a.Error_at_Project__cEnd_Date__c}"/>
<apex:column value="{!a.Error_at_Project__c}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>pageBlockTable>
</apex:pageBlock>pageBlockSection>
</apex:pageBlock>
</apex:form>pageBlock>
</apex:page>form>
controller
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<Project_Task__c> pTasklist{get;set;}
public Project__c p{get;set;}
public List<Project__c> pList{get;set;}
public List<Project__c> Projectlist{get;set;}
public List<Project__c> duplicaterecordsList{get;set;}
public List<Project__c> Project1{get;set;}
public List<Project__c> Project2{get;set;}
public List<Project_Task__c> Project12{get;set;}
public importDataFromCSVController(){
csvFileLines = new String[]{};
pTasklist = New list <Project_Task__c>();
duplicaterecordsList = New list <Project__c>();
}
public void importCSVFile(){
list<Project__c> pList1 = [select Id, Name,Error_at_Project__c,Start_Date__c,Total_Hours_Worked__c,End_Date__c,Client_Name__c,Developer_Name__c,Date__c from Project__c]; // for ascending order on Name
system.debug('---pList1---'+pList1.size());
try{
pList = new List<Project__c>();
String csvBody = csvFileBody.toString();
csvFileLines = csvBody.split('\n');
system.debug('---csvFileLines.size()----'+csvFileLines.size());
for(Integer i=1;i<csvFileLines.size();i++){
//Create Project and set the values
system.debug('--Line No---'+i);
string[] csvRecordData = csvFileLines[i].split(',');
system.debug('--csvRecordData---'+csvRecordData);
p = new Project__c() ;
p.Error_at_Project__c = csvRecordData[8] ;
p.Developer_Name__c = csvRecordData[4] ;
p.Name = csvRecordData[6] ;
p.Client_Name__c = csvRecordData[5] ;
try{
p.Date__c =Date.valueOf(csvRecordData[9]);
}catch (Exception preojectExc){
p.Error_at_Project__c=preojectExc.getMessage()+'Kindly check Project date';
}
try{
p.End_Date__c=Date.valueOf(csvRecordData[11]);
} catch (Exception preojectExc){
p.Error_at_Project__c=preojectExc.getMessage()+'Kindly check Project End date';
}
try{
p.Total_Hours_Worked__c =Integer.valueOf(csvRecordData[7]) ;
}catch (Exception preojectExc){
p.Error_at_Project__c=preojectExc.getMessage()+'Kindly check Project Hours';
}
try{
p.Start_Date__c=Date.valueOf(csvRecordData[10]);
}
catch (Exception preojectExc){
p.Error_at_Project__c=preojectExc.getMessage()+'Kindly check Project start date';
}finally{
insert p;
pList.add(p);
for(Project__c p1 : pList1){
if(p1.Name == p.Name ){
duplicaterecordsList.add(p1);
}
}
}
Project_Task__c pjTask = new Project_Task__c() ;
pjTask.Name = csvRecordData[0] ;
pjTask.Effort_Hours__c = Integer.valueOf(csvRecordData[1]) ;
pjTask.Error_at_Task__c = csvRecordData[2] ;
pjTask.Task_Name_and_Details__c = csvRecordData[3] ;
pjTask.Project_Name__c = p.Id;
pTasklist.add(pjTask);
}
Projectlist = [select Id, Name,Error_at_Project__c,Start_Date__c,Total_Hours_Worked__c,End_Date__c,Client_Name__c,Developer_Name__c,Date__c from Project__c where id=:pList]; // for ascending order on Name
system.debug('-------Projectlist---------'+Projectlist);
Projectlist.sort();
insert pTasklist;
Project2 = [Select Id,Name,Error_at_Project__c,Start_Date__c,Total_Hours_Worked__c,End_Date__c,Client_Name__c,Developer_Name__c,Date__c ,(Select Id,Name From Project_Tasks__r) From Project__c Where Id=:pList];
System.debug('Has child records.'+Project2);
system.debug('Child of project====>'+Project2[0].Project_Tasks__r);
}
catch (Exception e)
{
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
ApexPages.addMessage(errorMessage);
system.debug('----errorMessage----'+e);
}
}
}
</apex:page>