You need to sign in to do that
Don't have an account?
Rinigrace Mekala
CSV data import
Hi,
I want to import some data into salesforce using a csv file. For that i am using below code.
Now, I want to do some data validations while importing the file, such as 'if csv file does not contain data' and 'If the file extension is not csv' the user should see an error.
And every time the user clicks on the Custom button, the page should get refreshed.
Please guide me in this.
The apex class is:
public class Controller {
public List<Parent__c> parentlist{get;set;}
public List<Child__c> childlist{get;set;}
public String conType{get;set;}
public Blob namefile{get;set;}
public string str{get;set;}
public String[] fileLines{get;set;}
public Controller ()
{
conType = 'text/html';
}
public void importcsv()
{
Set<String> a1= new Set<String>();
conType = 'text/html';
fileLines = new String[]{};
parentlist = New List<Parent__c>();
childlist = New List<Child__c>();
Set<String> s1= new Set<String>();
try{
if (namefile==NULL){
ApexPages.addmessage(new ApexPages.Message(ApexPages.Severity.ERROR,'No file is choosen'));
}
else{
str = namefile.toString();
fileLines = str.split('\n');
string[] csvdata;
Parent__c parentob;
Child__c childob;
for(Integer i=1;i<fileLines.size();i++)
{
csvdata = fileLines[i].split(',');
system.debug('csvdata[0]'+csvdata[0]);
a1.add(csvdata[0]);
s1.add(csvdata[1]);
}
getA1Name(a1);
getS1Name(s1);
for(Integer i=1;i<fileLines.size();i++)
{
for(Parent__c p1:[SELECT id,A__r.name ,B__r.name, C__c
FROM Parent__c
WHERE id =:parentlist])
{
csvdata = fileLines[index].split(',');
if(p1.A__r.name== (csvdata[0]) && p1.B__r.name == (csvdata[1]) && p1.C__c == (decimal.valueOf(csvdata[2].trim())))
{
childob = new Child__c();
childob.Parent__c =p1.id;
//fields assignment
}
}
}
upsert childlist;
system.debug('FIND2...'+childlist);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Data Imported Succesfully'));
}
}
catch (Exception e)
{
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,e.getLineNumber()+'\n'+e.getMessage()+'\n'+e.getStackTraceString()+'\n'+e.getTypeName());
ApexPages.addMessage(errorMessage);
}
}
}
The VF page is:
<apex:page controller="DpsForecastingController" contentType="{!conType}">
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" lang="en">
<head>
<apex:stylesheet value="{!URLFOR($Resource.SLDS212, 'assets/styles/salesforce-lightning-design-system-vf.min.css')}" />
</head>
<body>
<apex:form >
<apex:pagemessages />
<apex:pageBlock >
<apex:pageBlockSection >
<apex:outputLabel >
<apex:inputFile value="{!namefile}"/>
<apex:commandButton value="Import file" action="{!importcsv}"/>
</apex:outputLabel>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock>
<apex:pageblocktable value="{!childList}" var="child" >
<apex:column value="{!child.Child__r.A__r.name}"/>
<apex:column value="{!child.Child__r.B__r.name}"/>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</body>
</html>
</apex:page>
Any kind of suggestion is appreciable. Thanks in advance.
I want to import some data into salesforce using a csv file. For that i am using below code.
Now, I want to do some data validations while importing the file, such as 'if csv file does not contain data' and 'If the file extension is not csv' the user should see an error.
And every time the user clicks on the Custom button, the page should get refreshed.
Please guide me in this.
The apex class is:
public class Controller {
public List<Parent__c> parentlist{get;set;}
public List<Child__c> childlist{get;set;}
public String conType{get;set;}
public Blob namefile{get;set;}
public string str{get;set;}
public String[] fileLines{get;set;}
public Controller ()
{
conType = 'text/html';
}
public void importcsv()
{
Set<String> a1= new Set<String>();
conType = 'text/html';
fileLines = new String[]{};
parentlist = New List<Parent__c>();
childlist = New List<Child__c>();
Set<String> s1= new Set<String>();
try{
if (namefile==NULL){
ApexPages.addmessage(new ApexPages.Message(ApexPages.Severity.ERROR,'No file is choosen'));
}
else{
str = namefile.toString();
fileLines = str.split('\n');
string[] csvdata;
Parent__c parentob;
Child__c childob;
for(Integer i=1;i<fileLines.size();i++)
{
csvdata = fileLines[i].split(',');
system.debug('csvdata[0]'+csvdata[0]);
a1.add(csvdata[0]);
s1.add(csvdata[1]);
}
getA1Name(a1);
getS1Name(s1);
for(Integer i=1;i<fileLines.size();i++)
{
for(Parent__c p1:[SELECT id,A__r.name ,B__r.name, C__c
FROM Parent__c
WHERE id =:parentlist])
{
csvdata = fileLines[index].split(',');
if(p1.A__r.name== (csvdata[0]) && p1.B__r.name == (csvdata[1]) && p1.C__c == (decimal.valueOf(csvdata[2].trim())))
{
childob = new Child__c();
childob.Parent__c =p1.id;
//fields assignment
}
}
}
upsert childlist;
system.debug('FIND2...'+childlist);
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Data Imported Succesfully'));
}
}
catch (Exception e)
{
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,e.getLineNumber()+'\n'+e.getMessage()+'\n'+e.getStackTraceString()+'\n'+e.getTypeName());
ApexPages.addMessage(errorMessage);
}
}
}
The VF page is:
<apex:page controller="DpsForecastingController" contentType="{!conType}">
<html xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" lang="en">
<head>
<apex:stylesheet value="{!URLFOR($Resource.SLDS212, 'assets/styles/salesforce-lightning-design-system-vf.min.css')}" />
</head>
<body>
<apex:form >
<apex:pagemessages />
<apex:pageBlock >
<apex:pageBlockSection >
<apex:outputLabel >
<apex:inputFile value="{!namefile}"/>
<apex:commandButton value="Import file" action="{!importcsv}"/>
</apex:outputLabel>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:pageBlock>
<apex:pageblocktable value="{!childList}" var="child" >
<apex:column value="{!child.Child__r.A__r.name}"/>
<apex:column value="{!child.Child__r.B__r.name}"/>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</body>
</html>
</apex:page>
Any kind of suggestion is appreciable. Thanks in advance.