You need to sign in to do that
Don't have an account?
Sales person 375
import unique accounts from csv file using Visualforce page
Im new to salesforce, im working on visualforce page to import Account and contacts to salesforce.
here is my requiremnt, please guide me to achive this.
Give the customer ability to import Accounts from external csv file to salesforce with button on custom Visualforce page.
Read the csv file and display the content in the Tabular form in the same Visualforce page.
Verify the Duplicate records based on Phone number, if the phone number of the import record matches with existing Account record then verify for Email field match, if matches then populate error message as Duplicate record on Visualforce page Table under Status column for particular record. Otherwise insert record and update the status column on Visualforce page as Success.
Extend the functionality to multiple Objects (Account, Contact, and Opportunity) from single csv file.
here is my code: im getting error "common.apex.runtime.impl.ExecutionException: List index out of bounds: 5" and status is not getting updated.
Please help me
VF:
<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:pageBlock >
<apex:pageblocktable value="{!accList}" var="acc">
<apex:column value="{!acc.name}" />
<apex:column value="{!acc.AccountNumber}" />
<apex:column value="{!acc.Type}" />
<apex:column value="{!acc.Accountsource}" />
<apex:column value="{!acc.Industry }" />
<apex:column headerValue="Status" >
<apex:outputText id="status"/>
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class:
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public string csvStatus{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
public string[] csvRecordData;
public importDataFromCSVController(){
csvFileLines = new String[]{};
acclist = New List<Account>();
}
public void importCSVFile()
{
try{
csvAsString = csvFileBody.toString();
csvFileLines = csvAsString.split('\n');
for(Integer i=1;i<csvFileLines.size();i++)
{
Account accObj = new Account() ;
csvRecordData = csvFileLines[i].split(',');
accObj.name = csvRecordData[0] ;
accObj.accountnumber = csvRecordData[1];
accObj.Type = csvRecordData[2];
accObj.AccountSource = csvRecordData[3];
accObj.Industry = csvRecordData[4];
uploadStatus(accObj);
csvRecordData[5] = csvStatus;
}
insert acclist;
}
catch (Exception e)
{
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'error here');
ApexPages.addMessage(errorMessage);
}
}
public List<Account> uploadStatus(Account a)
{
for(Account acc:[SELECT Name, Accountnumber FROM Account])
{
if(acc.name == a.name && acc.accountnumber == a.accountnumber)
{
this.csvStatus = 'Duplicate';
system.debug('inside if');
}
else
{
system.debug('inside else');
acclist.add(a);
this.csvStatus = 'unique';
}
}
return acclist;
}
}
here is my requiremnt, please guide me to achive this.
Give the customer ability to import Accounts from external csv file to salesforce with button on custom Visualforce page.
Read the csv file and display the content in the Tabular form in the same Visualforce page.
Verify the Duplicate records based on Phone number, if the phone number of the import record matches with existing Account record then verify for Email field match, if matches then populate error message as Duplicate record on Visualforce page Table under Status column for particular record. Otherwise insert record and update the status column on Visualforce page as Success.
Extend the functionality to multiple Objects (Account, Contact, and Opportunity) from single csv file.
here is my code: im getting error "common.apex.runtime.impl.ExecutionException: List index out of bounds: 5" and status is not getting updated.
Please help me
VF:
<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:pageBlock >
<apex:pageblocktable value="{!accList}" var="acc">
<apex:column value="{!acc.name}" />
<apex:column value="{!acc.AccountNumber}" />
<apex:column value="{!acc.Type}" />
<apex:column value="{!acc.Accountsource}" />
<apex:column value="{!acc.Industry }" />
<apex:column headerValue="Status" >
<apex:outputText id="status"/>
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class:
public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public string csvStatus{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
public string[] csvRecordData;
public importDataFromCSVController(){
csvFileLines = new String[]{};
acclist = New List<Account>();
}
public void importCSVFile()
{
try{
csvAsString = csvFileBody.toString();
csvFileLines = csvAsString.split('\n');
for(Integer i=1;i<csvFileLines.size();i++)
{
Account accObj = new Account() ;
csvRecordData = csvFileLines[i].split(',');
accObj.name = csvRecordData[0] ;
accObj.accountnumber = csvRecordData[1];
accObj.Type = csvRecordData[2];
accObj.AccountSource = csvRecordData[3];
accObj.Industry = csvRecordData[4];
uploadStatus(accObj);
csvRecordData[5] = csvStatus;
}
insert acclist;
}
catch (Exception e)
{
ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'error here');
ApexPages.addMessage(errorMessage);
}
}
public List<Account> uploadStatus(Account a)
{
for(Account acc:[SELECT Name, Accountnumber FROM Account])
{
if(acc.name == a.name && acc.accountnumber == a.accountnumber)
{
this.csvStatus = 'Duplicate';
system.debug('inside if');
}
else
{
system.debug('inside else');
acclist.add(a);
this.csvStatus = 'unique';
}
}
return acclist;
}
}
I managed to spend a few minutes and put the code for you. Its working well (although it may not be idiot proof).
Now, you can obviously use the code below, but more importantly go ahead and ask me if you have any doubts on why we did something this way and not another.
Controller:
VF page:
Please mark as best answer if this is good.
Thank you.
Mann.
All Answers
https://docs.google.com/document/d/1xZBCe9tDMSZhLp8A49Nv3msVtNZ3aIUPV2UdX926rTw/edit?usp=sharing
The error is with the way you are handling csvStatus field.
I am writing a corrected code now. Hopefully will complete before sleeping :P
Thank you. :D you are awesome.
as far as contacts are concerned we took a business call to decide where a contact should reside. there were of course cases where it wasn't easy but nevertheless in favor of simplicity we accepted this as a risk.
besides we had a process in place for internal data audit that would attempt to keep the data clean.
I appologize for the delay. I will try to give you a working code by EOD today UTC+8hrs. Is that okay with you?
Regards
Mann
I managed to spend a few minutes and put the code for you. Its working well (although it may not be idiot proof).
Now, you can obviously use the code below, but more importantly go ahead and ask me if you have any doubts on why we did something this way and not another.
Controller:
VF page:
Please mark as best answer if this is good.
Thank you.
Mann.
its perfect. Thank you.
i want to know that only solution is to use wrapper class or we can use any other method to this senario. As per my understanding we should use wrapper class to populate date back on VF, just curious to know if there is any other approach to this.
Thankyou.